Regexp Metachar 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 represents a word boundary. It matches the position at the beginning or end of a word, rather than matching any actual characters.
Specifically, b matches one of the following three cases:
* **Word beginning**: If `b` appears before a letter or digit, or at the beginning of a string, it matches the start position of a word.
* **Word ending**: If `b` 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 `b` appears between two consecutive letters or digits, it won't match anything because there is no word boundary.
Here are some examples of the metacharacter **b**:
* The regular expression `bwordb` matches the entire word "word", but not "words" or "sword". [Try it Β»](#)
* The regular expression `bd+b` matches a complete number, such as "123", but not "abc123". [Try it Β»](#)
* The regular expression `b+b` matches a complete uppercase word, such as "HELLO", but not "hello". [Try it Β»](#)
Note that **b** is a zero-width assertion that doesn't match actual characters, only positions. Therefore, when you want to match an actual character, don't use **b**, but use other characters or character combinations instead.
## Example
// Use b for word matching
var patternWord =/bwordb/;
var textWord ='This is a word in a sentence.';
if(patternWord.test(textWord)){
document.write('Found matching word:', patternWord.exec(textWord));
}else{
document.write('No match found!');
}
[Try it Β»](#)
* * *
## Non-word Boundary B
In regular expressions, B is the opposite of **b**. It represents a non-word boundary, i.e., it matches positions that are not at word boundaries.
Specifically, **B** matches one of the following cases:
* **Inside a word**: If `B` appears between two consecutive letters or digits, it matches the position between these two characters, indicating they are not word boundaries.
* **Non-word beginning or ending**: If `B` appears before or after a letter or digit, it matches this position, indicating it's not the beginning or ending of a word.
Here are some examples of the metacharacter **B**:
* The regular expression `BwordB` matches "word" in "sword1", but not "password" or "words". [Try it Β»](#)
* The regular expression `Bd+B` matches "123" in "abc123def", but not "123" or "abc123". [Try it Β»](#)
* The regular expression `B+B` matches "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 only matches positions, not actual characters.
## Example
// Use B for non-word matching
var patternWord =/BwordB/;
// This one will find a match
var textWord ='This is a password123 in a sentence.';
// This one won't find a match
//var textWord = 'This is a password in a sentence.';
if(patternWord.test(textWord)){
document.write('Found matching word:', patternWord.exec(textWord));
}else{
document.write('No match found!');
}
[Try it Β»](#)
[ Regular Expression - Metacharacters](#)
YouTip