JavaScript RegExp Word Boundary Metacharacter (\b)

Beginner
6 min read
Updated: Oct 2026
3 examples
Live preview

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
UsePrefer
Whole word/\bword\b/
Inside a word\B
Word characters\w
True/false checkregex.test(str)

How to Use \b

Wrap the target token with boundaries when you need a whole-word match.

  1. Write the word or token you want (for example cat).
  2. Add \b on both sides: /\bcat\b/.
  3. Test with regex.test(str) or extract with str.match(...).
  4. Remember: \b is a position — it never eats a space or letter.
PieceRequired?Role
\b beforeFor whole-word startEdge before the first word character
TokenYesThe letters/digits you want to match
\b afterFor whole-word endEdge after the last word character
FlagsOptionali 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

Cheat Sheet

Keep these facts handy while writing whole-word patterns.

TopicDetail
Syntax/\bword\b/
MatchesA position (zero characters)
Word chars[A-Za-z0-9_] (= \w)
Opposite\B = not a word boundary
Common winAvoid matching inside catalog, scat
Underscore traphello_world is one word token for \b
StandardES3+ (everywhere)

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" */

JavaScript Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

Example 1 — Whole-Word Match

Require boundaries on both sides of the token.

JavaScript
const pattern = /\bcat\b/;

console.log(pattern.test("a cat"));    // true
console.log(pattern.test("the cat!")); // true
console.log(pattern.test("catalog"));  // false
console.log(pattern.test("scat"));     // false
Try It Yourself

How It Works

  1. Build the pattern. /\bcat\b/ means: boundary, then cat, then boundary.
  2. Spaces and punctuation help. In "a cat" and "the cat!", edges exist around cat.
  3. Longer tokens fail. In "catalog" and "scat", cat sits inside a larger word.
  4. Zero width. \b never consumes the space or ! — it only checks the position.

Example 2 — Whole Word vs Partial Match

Without \b, a substring match can hit inside longer words.

JavaScript
const text = "catalog";

console.log(/cat/.test(text));      // true  (substring)
console.log(/\bcat\b/.test(text));  // false (not a whole word)

console.log(/\bcat\b/.test("cat-dog")); // true (hyphen is non-word)
Try It Yourself

How It Works

  1. Loose match. /cat/ only looks for those three letters — "catalog" contains them.
  2. Bounded match. /\bcat\b/ also requires edges; the trailing alog blocks the end boundary.
  3. Hyphen helps. In "cat-dog", - is not a word character, so \b succeeds after cat.
  4. Pick on purpose. Use \b for search/replace of whole words; skip it when a substring is fine.

Example 3 — Extract Words with \b\w+\b

Combine a boundary with \w+ to pull tokens from a sentence.

JavaScript
const text = "the cat sat";
const words = text.match(/\b\w+\b/g);

console.log(words);
// [ "the", "cat", "sat" ]

console.log(/\bjs\b/i.test("Learn JS now")); // true
Try It Yourself

How It Works

  1. Pattern pieces. \b starts a word, \w+ takes one or more word chars, \b ends it.
  2. Global flag. g finds every word in the string.
  3. match returns an array. You get ["the", "cat", "sat"].
  4. Case-insensitive. /\bjs\b/i matches "JS" as a whole word.

Edge Cases & Pitfalls

Check these before relying on \b in real projects.

underscore

_ is a word character

hello_world is one token for \b. /\bworld\b/ does not match inside it.

unicode

Letters outside ASCII

Default \w / \b are ASCII-oriented. For Unicode letters, use the u flag with Unicode property escapes when you need them.

zero-width

\b never consumes characters

It only checks a position. Do not expect \b to “match the space” in the capture.

opposite

Need the inside of a word?

Use \B for positions that are not word boundaries (for example between two letters).

Browser & Runtime Support

The \b word-boundary metacharacter is core regex syntax (ES3+). Available in every browser and Node.js version.

✓ Baseline · Widely available

RegExp \b

Word boundaries work everywhere. No polyfill needed for everyday whole-word matching.

100% Universal support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
\b Excellent

Bottom line: Safe for every environment. Remember that default word characters are ASCII letters, digits, and underscore.

Performance Notes

ApproachCostBest for
/\bword\b/Very cheapWhole-word search and replace
/\b\w+\b/gO(n)Tokenizing simple ASCII text
Loose /word/O(n)When a substring match is intentional

Anchors like \b are inexpensive. Cost usually comes from the rest of the pattern (greedy quantifiers, backtracking), not from the boundary itself.

Key Takeaways

  • Role: \b marks a word edge (zero-width position).
  • Whole words: wrap tokens as /\bword\b/.
  • Word chars: [A-Za-z0-9_] — underscore joins tokens.
  • Opposite: use \B for non-boundary positions.

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.

Next: \r carriage return

Learn how the carriage-return metacharacter matches line-ending control characters.

\r tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful