Regular Expression - Metacharacters \b and \B
Regular Expression - Metacharacters
\b and \B are two special metacharacters in regular expressions used to match word boundaries.
Word Boundary \b
In regular expressions, \b is a special metacharacter that denotes a word boundary. It matches the position at the start or end of a word, without matching any actual character.
Specifically, \b matches one of the following three cases:
- Start of a word: if
\b\b appears before a letter or digit, or at the start of a string, it matches the starting position of a word. - End of a word: if
\b\b appears after a letter or digit, or at the end of a string, it matches the ending position of a word. - Inside a word: if
\b\b appears between two consecutive letters or digits, it matches nothing, because there is no word boundary.
The following are some metacharacter \b examples:
- Regular expressions
\bword\bMatches the whole word "word", but not "words" or "sword".Try it » - Regular expressions
\b\d+\bMatches a complete number, such as "123", but not "abc123".Try it » - Regular expressions
\b[A-Z]+\bMatches a complete uppercase word, such as "HELLO", but not "hello".Try it »
Note that the\b is a zero-width assertion that does not match an actual character but only a position. Therefore, when you want to match an actual character, do not use \b; instead, use other characters or character combinations.
Example
// Use \b for word matching
var
patternWord
=
/\bword\b/
;
var
textWord
=
'This is a word in a sentence.'
;
if
(
patternWord.
test
(
textWord
)
)
{
document.
write
(
'Found matching word:'
,
patternWord.
exec
(
textWord
)
[
0
]
)
;
}
else
{
document.
write
(
'No matching content found!'
)
;
}
Try it »
Non-word Boundary \B
In regular expressions, \B is \b the opposite symbol of \b, representing a non-word boundary, that is, a position that is not at a word boundary.
Specifically,\B \B matches one of the following cases:
- Inside a word: if
\Bwhen it appears between two consecutive letters or digits, it matches the position between these two characters, indicating they are not a word boundary. - Not at the start or end of a word: if
\B\B appears before or after a letter or digit, it matches this position, indicating it is not the start or end of a word.
The following are some metacharacter \B examples:
- Regular expressions
\Bword\BMatches "word" in "sword1", but not "password" or "words".Try it » - Regular expressions
\B\d+\BMatches "123" in "abc123def", but not "123" or "abc123". Try it » - Regular expressions
\B[A-Z]+\BMatches "ELL" and "ORL" in "HELLO WORLD", but not "HELLO" or "WORLD". Try it »
Note that, unlike \b , \B is also a zero-width assertion that matches only positions and not actual characters.
Example
// Use \B for non-word matching
var
patternWord
=
/\Bword\B/
;
// This one is found
var
textWord
=
'This is a password123 in a sentence.'
;
// This one is not found
//var textWord = 'This is a password in a sentence.';
if
(
patternWord.
test
(
textWord
)
)
{
document.
write
(
'Found matching word:'
,
patternWord.
exec
(
textWord
)
[
0
]
)
;
}
else
{
document.
write
(
'No matching content found!'
)
;
}
Try it »