Explainer 6 min read

Regex Word Boundary \b Explained: Match Whole Words Only

What \b means in regex, how to match whole words, \b vs \B, and why apostrophes, hyphens, symbols like C++ and accented letters break word boundaries.

regex word boundarywhat does \b mean in regexregex match whole wordregex \b exampleword boundary javascript regex

The short answer

\b is a word boundary: a zero-width position between a word character (a letter, digit or underscore) and a non-word character, or the start or end of the text. Wrapping a word in \b...\b makes the regex match it only as a whole word: \bcat\b finds "cat" but not the "cat" inside "category" or "bobcat". You can try every example below in the regex tester with the g flag.

What counts as a "word character"

In JavaScript (which the Lazyblink regex tester uses) and in most regex engines, \w means exactly these 63 characters:

  • a to z
  • A to Z
  • 0 to 9
  • the underscore _

Everything else is a non-word character: spaces, punctuation, hyphens, apostrophes, emoji, and, in JavaScript without the Unicode flag, accented letters like é and every non-Latin script.

\b matches at any position where one side is a word character and the other side is not. It does not consume a character. It is an anchor, like ^ and $.

Seeing it in action

Test string: cat category concat bobcat cat.

PatternMatchesWhy
cat5 matchesEvery "cat", including inside other words
\bcat\b2 matchesOnly the standalone words at the start and before the full stop
\bcat3 matchesWords that start with cat: cat, category, cat
cat\b4 matchesWords that end with cat: cat, concat, bobcat, cat

The last "cat" matches \bcat\b because the full stop after it is a non-word character, so there is a boundary between "t" and ".".

Try it

  • Open the regex tester.
  • Type \bcat\b in the pattern box (no slashes needed) and g in the flags box.
  • Paste the test string. The tool highlights the two whole-word matches.
  • Common uses

    Find a whole word, case-insensitive

    Pattern \bis\b with flags gi on "This is it. Is it? island" matches "is" and "Is", but not the "is" inside "This" or "island".

    Match a number of an exact length

    \b\d{6}\b finds six-digit numbers such as Indian PIN codes. In "PIN 110001, ref 1100012, 560 001" it matches only 110001. The seven-digit reference is skipped because \b needs a boundary on both sides of the six digits, and "560 001" is two separate numbers.

    Allow an optional hyphen

    \bnon-?profit\b with gi matches both "nonprofit" and "non-profit". The hyphen is a non-word character, but it sits between "non" and "profit", inside your pattern, so it does not break the outer boundaries.

    Whole-word find and replace

    The Find and Replace tool has a Whole word checkbox. It does the same thing for you: it wraps your search in \b...\b. Replacing "cat" with "dog" with Whole word ticked leaves "category" untouched.

    \B: the opposite

    \B matches wherever \b does not: between two word characters or between two non-word characters.

    \Bcat\B on "cat category concat bobcat locate" matches only the "cat" inside "locate", because that is the only one with letters on both sides.

    Use \B to find text in the middle of words, for example a misspelling that only appears inside longer words.

    Where \b surprises people

    Apostrophes and hyphens split words

    \b\w+\b on "don't e-mail" finds four "words": don, t, e, mail. The apostrophe and the hyphen are non-word characters, so \b sees a boundary on each side of them.

    That also means \bre\b matches the "re" in "they're" and in "re-run". If you are searching for a short word, check whether it also appears after an apostrophe or before a hyphen.

    To treat "don't" as one word, use a pattern like \b[\w']+\b.

    Symbols at the edge of your word

    \bC\+\+\b does not match "C++" in "I know C++ and C#". After the last + comes a space. Both are non-word characters, so there is no boundary there.

    When your search term starts or ends with a symbol, drop \b on that side: \bC\+\+ works. Or use lookarounds such as (?<!\w) and (?!\w), explained in regex lookahead and lookbehind.

    Accented letters and other scripts

    In JavaScript without the u flag, é is a non-word character. So in "café cafés", the pattern \bcafé\b does not match the standalone word "café" (there is no boundary between "é" and the following space, because both are non-word characters). It matches the "café" at the start of "cafés" instead, because "é" is followed by the word character "s".

    For Hindi, Tamil, Arabic or any non-Latin script, \b is unreliable in JavaScript. Even with the u flag, JavaScript's \b and \w still only consider ASCII letters. Use explicit spaces or lookarounds with Unicode property escapes such as \p{L} (with the u flag) in your own code instead.

    Underscores are word characters

    \bname\b does not match inside user_name, because _ counts as a word character. That is helpful when searching code, but surprising in plain text.

    \b in other languages and tools

    WhereWord boundaryNotes
    JavaScript, Python, PHP, Java, .NET\bASCII \w by default in JavaScript; Python 3 str patterns treat Unicode letters as word characters
    grep -E / grep -P\bPlain grep -w also matches whole words
    MySQL 8 REGEXP\bMySQL 5.7 and earlier used different start-of-word and end-of-word markers
    PostgreSQL\yIn PostgreSQL, \b means backspace
    VS Code search\bOr the "Match Whole Word" button

    If a pattern works in the tester but not in your code, the difference is often one of these engine rules or a missing escape in a string. See regex works in the tester but not in my code.

    Escaping \b in code strings

    In a JavaScript regex literal you write /\bcat\b/. In a string passed to new RegExp, you must double the backslash: new RegExp("\\bcat\\b"). In Python, use a raw string: r"\bcat\b". Without the raw string, Python reads \b as a backspace character and the pattern silently fails.

    Common mistakes

  • Expecting \b to match a character. It matches a position. \bcat\b has a length of three characters, not five.
  • Putting \b next to a symbol. \b#tag fails at the start of a line or after a space. Use #\w+ or a lookbehind.
  • Forgetting the g flag in the tester. Without it, only the first match is shown.
  • Using \b on non-English text in JavaScript. Test carefully or use Unicode-aware lookarounds.
  • Unescaped backslashes in strings. "\bcat\b" in a normal JavaScript or Python string is not a word boundary.
  • Quick reference

    • \bword\b whole word only.
    • \bword words starting with "word".
    • word\b words ending with "word".
    • \B inside a word.
    • Word characters: A–Z a–z 0–9 _.
    • New to regex? Start with the regex beginners guide.

    Frequently asked questions

    What does \b mean in regex?

    \b is a word boundary: a zero-width position between a word character (letter, digit or underscore) and a non-word character, or the start or end of the text. It does not match any character itself.

    How do I match a whole word with regex?

    Wrap the word in word boundaries: \bcat\b matches 'cat' but not the 'cat' inside 'category' or 'bobcat'.

    What is the difference between \b and \B?

    \b matches at word boundaries. \B matches everywhere else, such as between two letters, so \Bcat\B finds 'cat' only in the middle of a word like 'locate'.

    Why does \bC\+\+\b not match C++?

    After the last + comes a space or the end of the text. Both sides are non-word characters, so there is no boundary. Drop the final \b or use a lookahead.

    Does \b work with accented letters or Hindi?

    Not reliably in JavaScript. \w only covers A–Z, a–z, 0–9 and underscore, so letters like é and all non-Latin scripts count as non-word characters.

    Try this tool on Lazyblink

    Put this guide into practice with our free online tool — no signup required.

    Open tool