Regex Word Boundary (\b) Explained

The \b anchor matches the position between a word character and a non-word character, without consuming any characters itself.

Published October 12, 2026

\b matches the position between a word character and a non-word character (or string edge). It lets you match whole words without matching parts of longer words.

How the pattern works

Breaking the pattern down: \b = word boundary (zero width); cat = the literal word.

Test your regex against both matching and non-matching inputs; use the Regex tool on this site to experiment live.

Pattern tests

/\bcat\b/
✓ 'the cat sat'
✓ 'cat'
✗ 'concatenate'
✗ 'category'

Practical examples

The pattern

const re = /\bcat\b/

Test it

const re = /\bcat\b/
re.test('the cat sat')   // true
re.test('cat')   // true
re.test('concatenate')   // false
re.test('category')   // false

In Python

import re
re.search(r'\bcat\b', text)

Common mistakes

  • Forgetting double backslashes in string patterns.
  • Expecting \b to match a character.
  • Unicode letters not treated as word characters.

Key takeaways

  • Anchors (^ and $) make the whole string match instead of a part of it.
  • Regex checks format only; verify data separately when it matters.
  • Test with valid and invalid samples.

FAQ

What is \B?

The opposite: a position that is not a word boundary.

Where can I test this pattern?

Use the Regex Tester on this site, or regex101.com.

More General articles