JavaScript RegExp Non-Word Boundary (\B)

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

What Is \B?

\B is a non-word-boundary assertion. It matches a zero-width position where \b does not match — usually between two word characters (inside a token) or between two non-word characters.

Remember
\b  → word boundary (word ↔ non-word, or string edge)
\B  → NOT a word boundary (inverse of \b)

zero-width — matches a position, not a character

/\Bcat\B/.test("scatter")  → true   (cat inside a word)
/\Bcat\B/.test("a cat")    → false  (cat is a whole word)
/\bcat\b/.test("a cat")    → true
UsePrefer
Substring inside a word/\B…\B/
Whole-word match/\b…\b/
Split camelCase/\B(?=[A-Z])/
Word characters\w

How to Use \B

Treat \B as a position check — place it where you need “still inside the same kind of characters.”

  1. Decide whether you want a whole word (\b) or something embedded (\B).
  2. Put \B before and/or after the text that must sit inside a larger token.
  3. Remember it consumes no characters — only the surrounding pattern advances.
  4. Combine with lookaheads for jobs like camelCase splits: \B(?=[A-Z]).
PieceRequired?Role
\B in the patternYesAssert “this position is not a word boundary”
Surrounding textUsuallyWhat you actually want to find (e.g. cat)
Second \BOptionalRequire non-boundary on the other side too
LookaheadOptionale.g. (?=[A-Z]) for camelCase cuts

Pseudocode

Pseudocode
at position p in the string:

left  = char before p (or "edge")
right = char after p  (or "edge")

isBoundary =
  (left is word) XOR (right is word)
  // edges count as non-word

if pattern has \b:
  accept only when isBoundary is true
if pattern has \B:
  accept only when isBoundary is false

// neither \b nor \B consumes a character

Cheat Sheet

Keep these facts handy when matching inside tokens.

TopicDetail
TypeZero-width assertion (not a character)
MeaningPosition where \b fails
Typical hitBetween two \w chars (inside a word)
Also hitsBetween two \W chars (e.g. inside "!!")
Embedded text/\Bcat\B/
camelCase splitstr.split(/\B(?=[A-Z])/)
StandardES3+ (universal)

Live Preview

Compare \B and \b on short sample strings.

Browser demo of the RegExp \B non-word-boundary assertion — same ideas as the examples below.

Result
text = "scatter"
/\Bcat\B/.test(text)
→ true

/* "cat" sits between letters — no word boundary */

JavaScript Examples

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

Example 1 — Match Embedded cat

Return true when cat appears inside a longer word, not as a standalone token.

JavaScript
const embedded = /\Bcat\B/;

console.log(embedded.test("scatter"));  // true
console.log(embedded.test("bobcatx"));  // true
console.log(embedded.test("a cat"));    // false
console.log(embedded.test("catalog"));  // false
Try It Yourself

How It Works

  1. Require both sides. \Bcat\B needs a non-boundary before and after cat.
  2. Inside a word. In "scatter" and "bobcatx", letters surround cat → match.
  3. Whole word fails. In "a cat", a space creates a \b before cat.
  4. Word edge fails. In "catalog", cat starts the token, so the left side is a boundary.

Example 2 — Contrast \b and \B

See the inverse behavior on the same sentence.

JavaScript
const sentence = "My cat loves scatter and catalog.";

console.log(sentence.match(/\bcat\b/g));
// ["cat"] — whole word only

console.log(sentence.match(/\Bcat\B/g));
// ["cat"] — inside "scatter" only

console.log(sentence.match(/cat/g));
// ["cat", "cat", "cat"] — all occurrences
Try It Yourself

How It Works

  1. Whole word. \bcat\b matches the pet name cat.
  2. Embedded only. \Bcat\B matches the cat inside scatter.
  3. No anchors. Plain /cat/g also hits catalog (word-start cat).
  4. Pick the right tool. Use \b for tokens, \B for insides.

Example 3 — Split camelCase With \B(?=[A-Z])

Break a camelCase identifier before each uppercase letter.

JavaScript
const id = "getUserAccountId";
console.log(id.split(/\B(?=[A-Z])/));
// ["get", "User", "Account", "Id"]

const label = "parseJSON";
console.log(label.split(/\B(?=[A-Z])/));
// ["parse", "J", "S", "O", "N"]  // acronyms split letter-by-letter
Try It Yourself

How It Works

  1. Position between letters. Lowercase then uppercase are both \w, so the spot between them is \B.
  2. Lookahead. (?=[A-Z]) splits only when the next character is uppercase.
  3. Clean camelCase. getUserAccountId becomes four parts.
  4. Acronym caveat. Consecutive capitals each trigger a split — see Edge Cases.

Edge Cases & Pitfalls

Check these before relying on \B in real projects.

zero-width

\B never consumes a character

It only asserts a position. Pair it with real pattern text (or a lookahead) or it will not “select” anything useful alone.

edges

Word edges are boundaries

\Bcat\B fails on "cat", "catfish", and "bobcat" because at least one side sits on a word edge.

acronym

camelCase + acronyms

/\B(?=[A-Z])/ splits before every capital, so "parseJSON" becomes ["parse","J","S","O","N"]. Use a richer pattern for acronyms.

digits

\B\d+\B is not “digits between letters”

It can also match an interior digit inside "456" (the "5"). Prefer lookarounds like (?<=[A-Za-z])\d+(?=[A-Za-z]) when you mean letters on both sides.

Browser & Runtime Support

The \B non-word-boundary assertion is core regex syntax (ES3+) and works everywhere JavaScript runs.

✓ Baseline · ES3+

RegExp \B

Supported in every modern and legacy browser, plus all Node.js versions. No polyfill required.

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 to use in any JavaScript environment. Lookbehind patterns for stricter “between letters” checks need modern engines (ES2018+).

Performance Notes

ApproachCostBest for
/\B…\B/O(n)Embedded substring checks
/\b…\b/O(n)Whole-word matching
split(/\B(?=[A-Z])/)O(n)Simple camelCase splits

Boundary assertions are cheap position checks. Prefer \b for whole words and \B when you intentionally want matches inside tokens.

Key Takeaways

  • Role: \B matches positions that are not word boundaries.
  • Opposite of \b: whole-word edges vs insides of tokens.
  • Zero-width: asserts a position; it does not consume characters.
  • Practical: embedded search with \B…\B, camelCase with \B(?=[A-Z]).

One line: \B means “not a word edge” — use it to match inside tokens; use \b for whole words.

Frequently Asked Questions

\B is a non-word-boundary assertion. It matches a zero-width position where \b would NOT match—typically between two word characters inside the same token, or between two non-word characters.
No. Like \b, \B is a zero-width assertion. It checks a position between characters (or at the start/end) without consuming any text.
\b matches word boundaries (where a word character meets a non-word character, or the string edges). \B is the inverse and matches positions that are not word boundaries—such as the middle of a word.
Use \B to find substrings embedded inside longer tokens (for example, cat inside scatter), or to split camelCase at uppercase letters with \B(?=[A-Z]).
No. Standalone cat has word boundaries on both sides. \Bcat\B requires non-boundaries on both sides, so it matches inside words like scatter or bobcatx, not a lone cat.
Non-word boundaries are core regex syntax since ES3. \B works in every browser and Node.js version.

Next: \0 null

Learn how the null-character escape matches U+0000 in JavaScript regex.

\0 tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

7 people found this page helpful