Boundaries: The \b and \B Assertions
The \b assertion matches at any
word boundary, defined as the position between a \w character and a \W character, in either order. If the order is
\W\w, it’s a beginning-of-word
boundary, and if the order is \w\W,
it’s an end-of-word boundary. (The ends of the string count as \W characters here.) The \B assertion matches any position that is
not a word boundary—that is, the middle of either
\w\w or \W\W.
/\bis\b/ # matches "what it is" and "that is it" /\Bis\B/ # matches "thistle" and "artist" /\bis\B/ # matches "istanbul" and "so—isn't that butter?" /\Bis\b/ # matches "confutatis" and "metropolis near you"
Because \W includes all
punctuation characters (except the underscore), there are \b boundaries in the middle of strings like
“isn't”, “booktech@oreilly.com”, “M.I.T.”, and “key/value”.
Inside a bracketed character class ([\b]), a \b
represents a backspace rather than a word boundary.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access