JavaScript RegExp Word Boundary Metacharacter (\b)
Beginner
6 min read
Updated: Oct 2026
3 examples
Live preview
Definition
What Is \b?
\b is a word-boundary anchor. It matches a position (zero width)—not a character—where a word character meets a non-word character, or the start/end of the string.
Remember
\b → word boundary (zero-width)
\B → NOT a word boundary
\w → word char: [A-Za-z0-9_]
/\bcat\b/ matches "a cat" — not "catalog"
/cat/ matches "catalog" (partial)
\b does not consume spaces or letters
Wrap the target token with boundaries when you need a whole-word match.
Write the word or token you want (for example cat).
Add \b on both sides: /\bcat\b/.
Test with regex.test(str) or extract with str.match(...).
Remember: \b is a position — it never eats a space or letter.
Piece
Required?
Role
\b before
For whole-word start
Edge before the first word character
Token
Yes
The letters/digits you want to match
\b after
For whole-word end
Edge after the last word character
Flags
Optional
i for case-insensitive; g for all matches
Pseudocode
Pseudocode
word char = letter, digit, or underscore
\b succeeds at a position when:
(left is word AND right is non-word)
OR (left is non-word AND right is word)
OR (start of string AND first char is word)
OR (end of string AND last char is word)
\b never advances the cursor (zero width)
/\bcat\b/ means:
boundary → "cat" → boundary
Reference
Cheat Sheet
Keep these facts handy while writing whole-word patterns.
Topic
Detail
Syntax
/\bword\b/
Matches
A position (zero characters)
Word chars
[A-Za-z0-9_] (= \w)
Opposite
\B = not a word boundary
Common win
Avoid matching inside catalog, scat
Underscore trap
hello_world is one word token for \b
Standard
ES3+ (everywhere)
Try it
Live Preview
Pick a pattern and see whether it matches with RegExp.prototype.test.
Browser demo of the RegExp \b word boundary — same ideas as the examples below.
Result
pattern = /\bcat\b/
text = "a cat"
pattern.test(text)
→ true
/* boundaries around the whole word "cat" */
Code
JavaScript Examples
Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.
One line:\b is a zero-width word edge — use /\bword\b/ for whole-word matches.
Frequently Asked Questions
\b is a word-boundary anchor. It matches a position—not a visible character—where a word character ([A-Za-z0-9_]) meets a non-word character, or the start/end of the string.
No. \b matches zero-width positions at word edges. In "a cat", \b can sit between the space and c, and after t—but it does not consume the space itself.
/cat/ matches cat anywhere inside a longer token (catalog, scat). \bcat\b requires cat to appear as a whole word with boundaries on both sides.
By default, word characters are letters A–Z and a–z, digits 0–9, and underscore _. Hyphens, spaces, and punctuation are non-word characters.
\B is the opposite of \b—it matches positions that are NOT word boundaries (for example, between two letters inside the same word).
Word boundaries are core regex syntax since ES3. \b works in every browser and Node.js version.