Word boundary assertion: \b, \B

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

A word boundary assertion checks if the current position in the string is a word boundary. A word boundary is where the next character is a word character and the previous character is not a word character, or vice versa.

Syntax

regex
\b
\B

Description

\b asserts that the current position in the string is a word boundary. \B negates the assertion: it asserts that the current position is not a word boundary. Both are assertions, so unlike other character escapes or character class escapes, \b and \B don't consume any characters.

A word character includes the following:

  • Letters (A–Z, a–z), numbers (0–9), and underscore (_).
  • If the regex is Unicode-aware and the i flag is set, other Unicode characters that get canonicalized to one of the characters above through case folding.

Word characters are also matched by the \w character class escape.

Out-of-bounds input positions are considered non-word characters. For example, the following are successful matches:

js
/\ba/.exec("abc");
/c\b/.exec("abc");

/\B /.exec(" abc");
/ \B/.exec("abc ");

Examples

Detecting words

The following example detects if a string contains the word "thanks" or "thank you":

js
function hasThanks(str) {
  return /\b(thanks|thank you)\b/i.test(str);
}

hasThanks("Thanks! You helped me a lot."); // true
hasThanks("Just want to say thank you for all your work."); // true
hasThanks("Thanksgiving is around the corner."); // false

Warning: Not all languages have clearly defined word boundaries. If you are working with languages like Chinese or Thai, where there are no whitespace separators, use a more advanced library like Intl.Segmenter to search for words instead.

Specifications

Specification
ECMAScript Language Specification
# prod-Assertion

Browser compatibility

BCD tables only load in the browser

See also