\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.

Frequently asked questions

How does Regular Expressions handle \b boundary?
Regular Expressions covers this with 2 copy-ready snippets on this page. The "\b Word Boundary" snippet in Regular Expressions uses `\bword\b`.
Which pattern does the Regular Expressions example use?
The "\b Word Boundary" snippet uses `\bword\b`, from the Basic Patterns section of the Regular Expressions cheat sheet.
What other Regular Expressions snippets are shown for "\b boundary"?
Besides "\b Word Boundary", this page also shows "\B Non-Word Boundary".
Is there anything to watch out for?
Yes. For "\b Word Boundary": 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').