ANCHORS & BOUNDARIES
Word boundaries
`\b` matches the edge of a word, so you can match whole words only.
\b matches the position between a word character (\w, meaning a letter, digit or underscore) and a non-word character - or the edge of the text. It marks the edge of a word and consumes nothing.
Why bother? A plain cat matches any cat - including the one buried inside concatenate, and the cat at the front of cats. Wrapping it as \bcat\b says "a word boundary, then cat, then a word boundary", so it only matches cat standing on its own. This is the difference between a find-and-replace that mangles your code and one that does not.
When does it actually change the result? It matters when your target sits flush against other word characters: concatenate, category and cats all contain the letters cat but are not the word cat, so \bcat\b rejects them while plain cat would match. It does NOT change anything when the target is already surrounded by spaces or punctuation - "the cat sat" and "cat, dog" match either way, because there is already a boundary on each side.
Because \b consumes nothing, "cat." still matches \bcat\b: the boundary sits in the gap between the t and the dot, and the dot stays out of the match.
The mirror of \b is \B: a position that is NOT a word boundary. \Bcat\B matches cat only when it sits buried inside a longer word, like "concatenate". MDN: \b and \B.
Match cat only as a whole word.
my cat sleeps
concatenate
the cat.
cats are not cat
Match is only as a whole word - not inside this, his or crisis.
this is it
is this a crisis
island
Match the identifier id only as a whole word - not inside userid or idle.
const id = 5
userid and idle
the id, the id