Non-Word Boundaries: \B
You'll learn how to use \B to match positions that are not at word boundaries, letting you find patterns buried inside words.
What \B Does
Remember \b from the previous lesson? It matches positions at the edge of words—where word characters meet non-word characters. \B is the opposite: it matches positions where you're either in the middle of a word or between two non-word characters.
Think of it like this: \b is the doorway between inside and outside a building. \B is anywhere that's not a doorway—either deep inside the building or completely outside in the parking lot.
Where \B Matches
\B succeeds in two situations:
- Between two word characters (inside a word):
a|p|ple(the pipes show\Bpositions) - Between two non-word characters:
#|@|!(also\Bpositions)
\B fails at actual word boundaries where \b would succeed.
Practical Use
const text = "Please update the software";
// Find "ware" only when inside a word
/\Bware/.test("software"); // true (inside "software")
/\Bware/.test("ware"); // false (at word start)
/\Bware/.test("beware"); // true (inside "beware")
// Find "up" only when NOT at a boundary
/\Bup/.test("update"); // false ("up" is at the start)
/\Bup/.test("setup"); // true ("up" is at the end, but preceded by word chars)
/\Bup\B/.test("Super"); // true ("up" is completely inside)
This is useful for finding substrings that must appear within larger words, not as standalone words or word beginnings.
Key Takeaway: \B matches positions that are not word boundaries—either between two word characters (inside words) or between two non-word characters, making it perfect for finding patterns embedded within words.