Word Boundaries \b
The \b anchor matches the boundary between a word character and a non-word character.
What is a Word Boundary?
A word boundary exists:
- Between a word character (
\w) and a non-word character (\W) - At the start/end of the string if it begins/ends with a word character
Only the standalone word "cat" matches, not "cat" inside other words.
Without Boundaries
Without \b, "cat" matches everywhere it appears, including inside words.
Exercises
Start and End Boundaries
You can use \b at just the start or end:
Non-Word Boundary
\B matches where \b doesn't - inside words.
Practical Examples
Practice Playground
Try:
\bjava\b- matches "java" only\bJava\b- matches "Java" only\b[Jj]ava\b- both cases\bJava\w*- words starting with Java
Key Takeaways
\bmatches word boundaries (edges of words)- Use
\bword\bto match whole words only \bprefixmatches words starting with prefixsuffix\bmatches words ending with suffix\Bmatches non-boundaries (inside words)

