Regex Word Boundary – Stop Matching Inside Longer Words

Local only

Fix a valid regex that finds a short token inside a longer word because no word boundaries were specified.

11 chars · 1 line

8 chars · 1 line

Pattern did not match the inputFlags: g

Regex boundary behavior

How to use word boundaries in a regular expression

The valid pattern cat matches the substring inside concatenate. When cat should count only as a standalone word, add \b boundaries around the token. This tool is free to use. No account or payment is required.

Last updated

What you can do here

  • Valid regex behavior check
  • Word boundary comparison
  • Substring debugging
  • Match or No match output
  • JavaScript RegExp semantics
  • Browser-local processing

Steps

  1. Reproduce the unwanted substring match with a longer word containing the token.
  2. Add \b before and after the token when JavaScript word-character boundaries match your requirement.
  3. Retest both standalone occurrences and embedded substrings.

Word boundary is different from start and end anchors

\b checks the transition between word and non-word characters. It can therefore find a standalone word inside a sentence, while ^ and $ are used when boundaries belong to the whole input or line.

Word-boundary regex example

Prevent cat from matching inside concatenate while still allowing the standalone word cat.

Substring pattern

cat

Word-boundary pattern

\bcat\b

Common questions

Frequently asked questions

Why does cat match concatenate?

Because a regex without boundaries can match the cat substring at any position.

Is \b a literal backspace here?

In regex pattern syntax, \b outside a character class represents a word boundary.

Does \b understand every Unicode word the way humans do?

JavaScript word-boundary behavior is based on regex word-character rules, so language-specific tokenization can require a different pattern.

Debugging a specific Regex issue? Browse Regex troubleshooting.