The invisible position between word and non-word
\b is a zero-width assertion that matches the boundary between a word character (\w) and a non-word character. It doesn't consume anything; it just asserts "this position is a word boundary."
\bcat\b matches the word cat in The cat sat, but NOT the cat in concatenate. Because in concatenate, c is preceded by another word character (so no boundary there) and t is followed by another word character (no boundary there either).
Where boundaries occur
A \b matches at:
- The start of the string, if the first character is a word character.
- The end of the string, if the last character is a word character.
- Between a word and a non-word character (in either order).
\B is the inverse — "NOT a word boundary." Useful for matching only inside words: \Bcat\B matches cat in concatenate but not in The cat sat.
Unicode caveats again
Word boundaries depend on the definition of \w, which varies by flavor and flag. In ASCII-only mode, \b won't recognize Korean text as words because 안녕 has no \w characters in ASCII mode. Switch to Unicode mode (Python default; u flag in JS) and it works.