\b boundary

2 snippets in Regular Expressions

Also written as \B boundary

RXRegular Expressions

\b Word Boundary

RX · Basic Patterns
syntax
\bword\b
example
JS:  'catfish is not a cat'.match(/\bcat\b/g)
Py:  re.findall(r'\bcat\b', 'catfish is not a cat')
output
["cat"]  (matches only the standalone word, not "catfish")

Note A word boundary is the position between a word character (\w) and a non-word character. Essential for whole-word searches. In JS, remember to double-escape in strings: new RegExp('\\bcat\\b').

\B Non-Word Boundary

RX · Basic Patterns
syntax
\Bpattern  or  pattern\B
example
JS:  'catfish is not a cat'.match(/\Bcat/g)
Py:  re.findall(r'\Bcat', 'catfish is not a cat')
output
[]  (no match -- 'cat' always starts at a word boundary here)

'scattered'.match(/\Bcat\B/g) => ["cat"]  (inside a word)

Note \B matches every position that is NOT a word boundary -- useful for finding substrings embedded within larger words.