Regular Expressions - Metacharacters \b and \B
Regular Expressions - Metacharacters
\band\BThey are two special metacharacters in regular expressions, used to match word boundaries.
Word Boundary \b
In regular expressions,\bis a special metacharacter that represents a word boundary. It matches the position at the start or end of a word, without matching any actual characters.
Specifically,\bit matches one of the following three situations:
- Start of a word: if
\bit appears before a letter or digit, or at the beginning of a string, it matches the start position of a word. - End of a word: if
\bit appears after a letter or digit, or at the end of a string, it matches the end position of a word. - Inside a word: if
\bit appears between two consecutive letters or digits, it will not match anything because there is no word boundary.
The following are some metacharacter\bexamples:
- Regular expression
\bword\bMatches the whole word "word", but not "words" or "sword".Try it » - Regular expression
\b\d+\bMatches a complete number, e.g., "123", but not "abc123".Try it » - Regular expression
\b[A-Z]+\bMatches a complete capitalized word, e.g., "HELLO", but not "hello".Try it »
It should be noted that\bis a zero-width assertion. It does not match actual characters, only positions. Therefore, when you want to match an actual character, do not use\b, but should use other characters or character combinations.
Examples
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,\BYes\bis the opposite symbol, representing a non-word boundary, i.e., matching positions that are not at word boundaries.
Specifically,\Bit matches one of the following situations:
- Inside a word: if
\Bit appears between two consecutive letters or digits, it matches the position between those two characters, indicating that they are not word boundaries. - Not at the start or end of a word: if
\Bit appears before or after a letter or digit, it matches that position, indicating that it is not the start or end of a word.
The following are some metacharacter\Bexamples:
- Regular expression
\Bword\BMatches "word" in "sword1", but not "password" or "words".Try it » - Regular expression
\B\d+\BMatches "123" in "abc123def", but not "123" or "abc123".Try it » - Regular expression
\B[A-Z]+\BMatches "ELL" and "ORL" in "HELLO WORLD", but not "HELLO" or "WORLD".Try it »
It should be noted that, unlike\b,\Bis also a zero-width assertion, matching only positions and not actual characters.
Examples
var patternWord = /\Bword\B/;
// This one matches
var textWord = 'This is a password123 in a sentence.';
// This one does not match
//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 »