Hone

Lessons · Regex · word boundaries

The edge of a word

\b matches the position between a word character and a non-word one; \bcat\b is the whole word cat.

Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.

What it is for

Find-and-replace of a variable name without touching names that contain it.

How to think about it

Whole word, or anywhere? Whole-word matches: \b on both sides. Remember digits and underscore are word characters too.

Worked example

pattern: \bis\b
Boundary, the word, boundary.
text: this is it
Matches the middle 'is', not the one inside 'this'.

Your turn

Whole word 'log'.

log

The trap

\b next to punctuation: 'cat.' still has a boundary after t, because . is non-word.

Practise word boundaries on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.