The \b metacharacter marks a word boundary—a zero-width position between a word character and a non-word character. Wrap a term with \b on both sides to match whole words like cat without hitting catalog or scat.
01
Anchor
\b
02
Zero-width
No chars
03
Whole word
\bcat\b
04
Word chars
A–Z 0–9 _
05
Opposite
\B
06
Search
Find tokens
Fundamentals
Introduction
Searching for cat with a plain pattern matches cat, catalog, and scat. That is often too broad when you want an exact word.
Word boundaries solve this. The \b anchor sits at the edge of a word—where letters, digits, or underscores meet spaces, punctuation, or string ends. It does not consume characters; it only checks the position.
Concept
Understanding the \b Word Boundary
In JavaScript, a word character is [A-Za-z0-9_]. A word boundary exists when:
The current character is a word char and the previous is not (or start of string).
The current character is not a word char and the previous is a word char (or end of string).
Think of \b as an invisible fence between words and non-words. \bcat\b means: fence, then cat, then fence—so the match must stand alone as a token.
Usage
How to Use \b in JavaScript
Place \b before and after the word you want to isolate. Combine with flags and other regex tools as needed:
JavaScript
const wholeWord = /\bcat\b/;
wholeWord.test("The cat sat"); // true
const found = "big cat catalog".match(/\bcat\b/g);
console.log(found); // ["cat"]
"my cat and catalog".replace(/\bcat\b/g, "dog");
// "my dog and catalog"
Use test() for presence checks, match() to extract whole words, and replace() to swap only isolated tokens.
Foundation
📝 Syntax
JavaScript
\b // word boundary (start or end of word)
\bcat\b // whole word "cat"
\b(word1|word2)\b // whole-word alternation
\B // NOT a word boundary
Common patterns
/\bcat\b/ — match cat as a standalone word.
/\b\d+\b/g — match whole numbers (not digits inside words).
/\b(yes|no)\b/i — match yes or no as whole words, any case.
/\Bcat\B/ — match cat only when surrounded by word chars (inside a token).
Cheat Sheet
⚡ Quick Reference
Goal
Pattern
Whole word match
\bword\b
Word starts with…
\bword
Word ends with…
word\b
All whole numbers
\b\d+\b
Not a boundary
\B
Word characters
[A-Za-z0-9_] or \w
Compare
📋 \bcat\b vs /cat/ vs ^cat$
Three ways to control how strictly a term must appear.
Partial
/cat/
Anywhere inside
Whole word
\bcat\b
Token only
Entire string
^cat$
Full input
Inside word
\Bcat\B
Not at edge
Hands-On
Examples Gallery
Open DevTools Console (F12) or use Try-it links. Examples show whole-word detection, extraction, replacement, validation, and combined patterns.
📚 Getting Started
See how boundaries block partial matches inside longer tokens.
Example 1 — Test for Whole Word cat
Return true only when cat appears as its own word.
Without boundaries, match(/cat/g) also hits catalog. Boundaries keep the result to standalone tokens.
Example 3 — Replace Only the Whole Word
Swap cat for dog without altering catalog.
JavaScript
const sentence = "My cat loves the catalog.";
const safe = sentence.replace(/\bcat\b/g, "dog");
console.log(safe); // "My dog loves the catalog."
const unsafe = sentence.replace(/cat/g, "dog");
console.log(unsafe); // "My dog loves the dogalog."
Building \bword\b dynamically checks whether the term appears as a token inside a longer string. Use ^word$ when the entire input must equal the word exactly.
Example 5 — Match Whole Words with Alternation
Detect yes or no only when they appear as standalone words.
JavaScript
const answer = /\b(yes|no)\b/i;
const text = "Yes! Yesterday was nice. Say no now.";
const found = text.match(answer);
console.log(found); // ["Yes", "no"]
console.log(answer.test("yesman")); // false
console.log(answer.test("oh yes")); // true
For Unicode word boundaries, explore \p{L} with the u flag on modern engines.
Compatibility
Browser & Runtime Support
The \b word-boundary metacharacter is part of core JavaScript regular expression syntax.
✓ Baseline · ES3+
RegExp \b
Supported in every browser and Node.js version. Word-boundary behavior is consistent across all JavaScript runtimes.
99%Universal syntax
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
\bExcellent
Bottom line: Safe everywhere. Use \b whenever whole-word matching matters.
Wrap Up
Conclusion
The \b metacharacter marks word boundaries so you can match whole tokens instead of accidental substrings. Wrap terms with \b on both sides for precise search, replace, and validation.
Remember that boundaries are positions, not characters, and that underscores count as word characters. Next, learn the \r carriage return metacharacter for line-ending patterns.
Use ^word$ when the entire input must equal one word
Escape user input when building dynamic boundary patterns
❌ Don’t
Expect \b to match spaces or punctuation
Forget that _ is part of a word
Confuse \b with ^ or $ string anchors
Assume \b handles all Unicode letters by default
Replace without boundaries when substrings must be protected
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about RegExp \b
Match whole words at token boundaries.
5
Core concepts
🔲01
Boundary
\b
Syntax
🔍02
Whole word
\bcat\b
Pattern
📈03
Zero-width
No chars.
Concept
🛠04
Word chars
A–Z 0–9 _
Rules
🔄05
Opposite
\B
Compare
❓ 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 between t and the following space—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.
Did you know?
In a string like "price: $5", a boundary exists between e and : because : is not a word character. That is why \b5\b can match the standalone digit 5 but not the 5 inside $50 (where 5 is preceded by $, also non-word).