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.

No boundary — matches inside other words

Try this
/cat/g

Input

The cat sat on the concatenation.

Result

Matches: cat (from 'cat'), cat (from 'concatenation')

With word boundary — only standalone matches

Try this
/\bcat\b/g

Input

The cat sat on the concatenation.

Result

Match: cat (from 'cat' only)

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

All reference guidesOpen the RegexPro tester →