RegexBuilder
← Back to the tools
📍

Regex Anchors: ^, $ and Word Boundaries (\b)

Anchors are zero-width: they match a position, not a character. ^ is the start of the string, $ is the end. They are how you stop a pattern from matching in the middle of a longer word.

^ and $ pin the edges

^cat matches a line that begins with cat. cat$ matches a line that ends with cat. ^cat$ matches only the exact word cat and nothing else. Anchoring is the difference between "contains" and "is exactly".

The multiline flag changes their meaning

By default ^ and $ refer to the whole string. With the multiline flag (m in JavaScript, re.MULTILINE in Python), they match the start and end of every line instead — essential when validating each row of a block of text.

\b matches a word boundary

\b is a position between a word character (\w) and a non-word character. \bcat\b matches cat but not catfish or concat, because cat there is glued to other letters. \B matches the opposite: a position not at a word boundary.

Boundaries in practice

Search for a whole word: \bword\b. Find a hex color or an IP address only when it stands alone: \b\d{1,3}(\.\d{1,3}){3}\b. Boundaries keep your matches from bleeding into surrounding text.

Test and explain patterns free