JavaScript RegExp \B Metacharacter

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Non-word boundary

What You’ll Learn

The \B metacharacter matches a non-word-boundary position—a zero-width spot where \b would not match. Use it to find substrings embedded inside longer tokens, digits sandwiched between letters, or camelCase split points.

01

Non-boundary

\B

02

Zero-width

No characters consumed

03

Inverse

Opposite of \b

04

Inside word

\Bcat\B

05

Digits

\B\d+\B

06

camelCase

\B(?=[A-Z])

Introduction

You already know that \b finds word edges. The uppercase form \B does the opposite: it matches positions that are not at a word boundary—typically the middle of a token where word characters sit on both sides.

Because \B is zero-width, it does not consume characters. It only checks whether the current position qualifies before the engine continues matching the rest of the pattern.

Understanding the \B Metacharacter

A word boundary (\b) sits where a word character ([A-Za-z0-9_]) meets a non-word character, or at the start/end of the string next to a word character. A non-word boundary (\B) matches everywhere else.

JavaScript
/\bcat\b/.test("a cat");     // true  (whole word)
/\bcat\b/.test("scatter");   // false (inside word)

/\Bcat\B/.test("scatter");   // true  (embedded)
/\Bcat\B/.test("a cat");     // false (has boundaries)

/\B/.test("ab");             // true  (between a and b)

Think of \B as “not at a word edge.” It is essential when you need the inverse of whole-word matching.

💡
Beginner Tip

If \bcat\b finds standalone cat, then \Bcat\B finds cat buried inside a longer word like scatter or bobcatx.

How to Use \B in JavaScript

Use \B when you need to match inside tokens rather than at their edges:

JavaScript
/\Bthe\B/.test("other");           // true
"scatter".match(/\Bcat\B/);       // ["cat"]
"item123sale".match(/\B\d+\B/);    // ["123"]
"myVariable".split(/\B(?=[A-Z])/);// ["my", "Variable"]

Pair \B with lookahead ((?=[A-Z])), character classes, and quantifiers for precise inside-token patterns.

📝 Syntax

JavaScript
\B              // one non-word-boundary position
\Bword\B        // word embedded inside a token
\B\d+\B         // digits between word characters
\B(?=[A-Z])      // before uppercase in camelCase
\b              // word boundary (inverse of \B)

Common patterns

  • /\Bcat\B/ — match cat only when embedded inside a word.
  • /\B\d+\B/g — extract digit runs sandwiched between letters.
  • /\B(?=[A-Z])/g — split camelCase identifiers.
  • /\Bthe\B/gi — find “the” inside words like “other” or “brother”.

⚡ Quick Reference

GoalPattern
Non-boundary position\B
Substring inside word\Bword\B
Embedded digits\B\d+\B
Split camelCase/\B(?=[A-Z])/g
Whole word (opposite)\bword\b
Inverse anchor\b\B

📋 \B vs \b

Two inverse zero-width anchors for word edges and non-edges.

Non-boundary
\B

Inside tokens

Word boundary
\b

At word edges

Embedded
\Bcat\B

Inside word

Whole word
\bcat\b

Standalone

Examples Gallery

Open DevTools Console (F12) or use Try-it links. Examples contrast \b and \B, find embedded substrings, extract inline digits, and split camelCase.

📚 Getting Started

See how \B matches inside words where \b fails.

Example 1 — Test for 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

In "scatter", letters surround cat on both sides—no word boundary at either edge. In "a cat", spaces create boundaries, so \Bcat\B fails.

📈 Practical Patterns

Extract, contrast, and parse with non-word-boundary anchors.

Example 2 — Find the Inside Longer Words

Match the substring only when it is embedded, not at a word edge.

JavaScript
const text = "The other brother went there.";

const embedded = text.match(/\Bthe\B/gi);
console.log(embedded); // ["the", "the"] (other, brother)

const whole = text.match(/\bthe\b/gi);
console.log(whole);    // ["The"] (standalone only)
Try It Yourself

How It Works

\Bthe\B hits the inside other and brother but skips standalone The at the sentence start because a word boundary precedes it.

Example 3 — Extract Digits Embedded in a Token

Pull number sequences sandwiched between letters, ignoring standalone numbers.

JavaScript
const sku = "item123sale costs 456 total";

const inline = sku.match(/\B\d+\B/g);
console.log(inline); // ["123"]

const all = sku.match(/\d+/g);
console.log(all);    // ["123", "456"]
Try It Yourself

How It Works

\B\d+\B requires a word character on both sides of the digit run. Standalone 456 has spaces around it (word boundaries), so it is excluded.

Example 4 — Contrast \b and \B Side by Side

See the inverse behavior on the same input string.

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

\bcat\b matches the pet name. \Bcat\B matches inside scatter. Plain /cat/g matches all three including catalog.

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

Break a camelCase identifier at uppercase letters using a non-boundary plus lookahead.

JavaScript
const id = "getUserAccountId";

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

const label = "myHTMLParser";
console.log(label.split(/\B(?=[A-Z])/));
// ["my", "HTMLParser"]
Try It Yourself

How It Works

Between a lowercase and uppercase letter, both characters are word chars—so the position is a \B (non-boundary). The lookahead (?=[A-Z]) splits only before capitals.

🚀 Common Use Cases

  • Embedded substring search — find text inside longer tokens with \B...\B.
  • Inline ID extraction — pull digits embedded in SKUs with \B\d+\B.
  • camelCase parsing — split identifiers at \B(?=[A-Z]).
  • Inverse whole-word logic — exclude standalone matches when \b is too broad.
  • Token analysis — detect positions inside compound words for NLP-style processing.
  • Refactoring tools — rename variables without hitting partial matches at word edges.

Important Considerations

  • Zero-width only\B matches positions, not characters. It adds no length to the result.
  • Inverse of \b — wherever \b succeeds, \B fails, and vice versa (at the same position).
  • Underscore counts — underscore is a word character, so a_b has \B between the letters and underscore.
  • Not a character class — do not confuse \B (non-boundary) with \W (non-word character).
  • Start/end edges — string start/end next to a word char is a \b position, not \B.

🧠 How \B Checks Positions

1

Reach position

The engine arrives at a zero-width point between characters (or at start/end).

Anchor
2

Test \b

Internally, \B succeeds when \b would fail at that spot.

Invert
3

Continue pattern

If \B passes, matching continues with the next token in the pattern.

Proceed
4

Match result

Inside-word positions pass; word-edge positions fail.

📝 Notes

  • \B is the uppercase inverse of \b.
  • Do not confuse \B (non-boundary anchor) with \W (non-word character).
  • Review \b word boundary for the complementary anchor.
  • Next up: \0 null character metacharacter.
  • Word characters are [A-Za-z0-9_] by default in JavaScript.
  • For whole-word replacement, prefer \bword\b; for inside-word matches, use \Bword\B.

Browser & Runtime Support

The \B non-word-boundary anchor is part of core JavaScript regular expression syntax.

Baseline · ES3+

RegExp \B

Supported in every browser and Node.js version. \B is the standard inverse of the \b word-boundary anchor.

99% Universal syntax
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 everywhere. Use \B when you need positions that are not at word edges.

Conclusion

The \B metacharacter matches non-word-boundary positions—the inverse of \b. It is zero-width, consumes no characters, and excels at finding substrings embedded inside tokens.

Use \Bword\B for inside-word matches, \B\d+\B for inline digits, and \B(?=[A-Z]) for camelCase splitting. Next, learn the \0 null character metacharacter.

💡 Best Practices

✅ Do

  • Pair \B with both sides of a substring: \Bcat\B
  • Use \B(?=[A-Z]) for camelCase tokenization
  • Contrast with \b to verify edge vs inside behavior
  • Remember \B is zero-width (no characters matched)
  • Learn \b first, then \B as its inverse

❌ Don’t

  • Confuse \B with \W (different concepts)
  • Expect \B to match visible characters
  • Use \Bcat\B when you mean whole-word \bcat\b
  • Forget underscore is a word character in boundary checks
  • Assume \B works the same as negating \b in all regex flavors (verify in JS)

Key Takeaways

Knowledge Unlocked

Five things to remember about RegExp \B

Match positions that are not at word edges.

5
Core concepts
🚫 02

Zero-width

No chars.

Anchor
📈 03

\Bcat\B

Embedded.

Pattern
🔢 04

\B\d+\B

Inline nums.

Extract
📚 05

\b

Inverse.

Pair

❓ Frequently Asked Questions

\B is a non-word-boundary anchor. 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 word and non-word characters meet). \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), digits sandwiched between letters, or to split camelCase at uppercase letters with \B(?=[A-Z]).
No. Standalone cat has word boundaries on both sides. \Bcat\B requires cat to be surrounded by word characters on both sides, so it matches inside words like scatter or bobcatx but not a cat by itself.
Non-word boundaries are core regex syntax since ES3. \B works in every browser and Node.js version.
Did you know?

JavaScript has four common zero-width anchors: ^ and $ for string start/end, plus \b and \B for word-boundary positions. None of them consume characters—they only test whether the current position qualifies.

Continue to \0 null character metacharacter

Learn how the null character escape matches U+0000 in strings.

\0 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.

6 people found this page helpful