A word boundary anchor, written \b, matches a position where a word character and a non-word character meet, or where the string begins or ends. It checks the position without consuming a character. For example, /\bword/ matches “word” in “!word!sword” because the exclamation mark begins a boundary, while /\Bword/ matches “word” inside “sword” because that position is not a boundary. section 11.5.3
Use the two related anchors to control whether a match may cross a word boundary:
- \b requires a transition between a \w character and a non-\w character, or a string edge. section 11.5.3
- Whitespace and other non-\w characters count as boundaries, so the anchor can distinguish a standalone word from the same letters inside a larger word. section 11.5.3
Match a word at its start
The pattern /\bword/ matches the first “word” in “!word!sword” but not the occurrence inside “sword”.


