Concept
Word Boundaries: \b and \B
\b matches the position between a word character and a non-word character. It keeps your regex from matching 'cat' inside 'concatenate.'
What \b matches
\b is a zero-width assertion that matches where one side is a word character ([A-Za-z0-9_]) and the other side isn't. Start of string + word, word + end of string, and word + punctuation all count. The boundary has no width — it doesn't consume a character, it just requires the transition.
Why you need it
Without \b, searching for 'cat' matches inside 'concatenate,' 'scatter,' and 'vacation.' Wrapping the word in \bcat\b restricts the match to standalone occurrences.
The inverse: \B
\B matches the opposite — a position NOT at a word boundary. It's useful for searches like 'find -ing at the end of a longer word' (\Bing\b) or catching hashtags inside larger strings.
Caveats
Unicode: \b in most JavaScript regex flavors only considers ASCII word characters. The u flag and \p{L} give you Unicode-aware alternatives. Hyphens and apostrophes count as non-word, so \bdon\b matches the 'don' in 'don't.'
Related patterns
Credit Card Number
Match 16-digit credit card numbers with optional spaces or hyphens between groups of 4.
/\b(?:\d{4}[\s\-]?){3}\d{4}\b/gHex Color Code
Match CSS hex color codes in both 3-digit (#RGB) and 6-digit (#RRGGBB) formats.
/#(?:[0-9a-fA-F]{6}|[0-9a-fA-F]{3})…/gUS ZIP Code
Match US ZIP codes in 5-digit (12345) and ZIP+4 (12345-6789) formats.
/\b\d{5}(?:[\-\s]\d{4})?\b/gDuplicate Word
Detects consecutive duplicate words using a backreference.
/\b(\w+)\s+\1\b/gi