4. Anchors & Boundaries
Matching positions, not characters
Everything we've covered so far matches characters — actual bytes in the text. Anchors are different. They match positions — the invisible spots between characters (or at the edges of the string). They have zero width: they don't consume any characters, they just assert "the match must be at this position."
Start and End of String
^ — Start of String
Pattern
^Hello
Input
Hello World
Matches
Hello World
Input
Say Hello
Result
No match — "Hello" is not at the start.
$ — End of String
Pattern
World$
Input
Hello World
Matches
Hello World
Input
World Cup
Result
No match — "World" is not at the end.
Combining ^ and $
Using both anchors together constrains the pattern to match the entire string:
Pattern
^\d{4}-\d{2}-\d{2}$
Matches
2026-04-24
Does NOT match
Date: 2026-04-24
2026-04-24 10:30
Why
The anchors require the date pattern to be the only thing in the string — no prefix, no suffix.
This is essential for input validation. Without ^ and $, a regex will happily find
a partial match inside a longer string.
Multiline Mode
By default, ^ and $ match the start/end of the entire string. With the multiline
flag (m), they match the start/end of each line.
Pattern (with /m flag)
^\d+
Input
100 apples
200 oranges
300 bananas
Matches
100 apples
200 oranges
300 bananas
Without /m
Only 100 would match — ^ only anchors to the start of the
whole string.
Word Boundaries: \b
The \b anchor matches at the boundary between a word character (\w) and a non-word
character (\W), or at the start/end of the string if it begins/ends with a word
character.
Pattern
\bcat\b
Input
The cat sat on the concatenation mat.
Matches
The cat sat on the concatenation mat.
Why
The \b anchors require "cat" to be a standalone word. The "cat" inside
"concatenation" is surrounded by word characters on both sides, so the boundaries don't
match.
Remember in the first article, plain cat matched inside "concatenation"? Word boundaries
solve that problem.
Where Boundaries Occur
Think of \b as matching at these positions:
- Between a
\wand a\Wcharacter - Between a
\wcharacter and the start/end of the string
Pattern
\b\d+\b
Input
abc123def 456 78x
Matches
abc123def 456 78x
Why
123 inside abc123def has word characters on both sides — no
boundary. 456 is surrounded by spaces. 78 in 78x
has a word character after it.
Wait — 456 matched but 78 didn't? That's because \b\d+\b requires word
boundaries on both sides. In 78x, there's a boundary before 7 (space-to-digit) but
not after 8 (digit-to-letter are both \w). And \d+ won't match 78x because x
isn't a digit.
\B — Non-Word Boundary
The opposite of \b. It matches at every position where \b does not match.
Pattern
\Bcat\B
Input
The cat sat on the concatenation mat. locate
Matches
The cat sat on the concatenation mat. locate
Why
\B requires both sides to be within a word — so only "cat" that's fully
embedded inside another word matches.
Absolute Anchors: \A and \Z
Some regex flavors provide \A (start of string) and \Z (end of string) that are
not affected by multiline mode. They always anchor to the very beginning and end of
the entire input, regardless of flags.
| Anchor | Meaning | Affected by /m? |
|---|---|---|
^ |
Start of string/line | Yes |
$ |
End of string/line | Yes |
\A |
Start of string (absolute) | No |
\Z |
End of string (absolute) | No |
Use \A and \Z when you specifically need string-level anchoring in a context where
multiline mode might be enabled.
Practical Patterns
Validate an entire input
^\S+@\S+\.\S+$
A rough email check — the ^...$ anchors ensure the whole string must match, not just a
substring.
Match whole words only
\b(error|warning|fatal)\b
Finds log levels as standalone words, not as substrings of "terrorism" or "warning-free."
Match lines starting with a comment
^(#|//).*$
With the m flag, this matches every line that starts with # or //.
Key Takeaways
- Anchors match positions, not characters. They consume zero characters.
^and$match start/end of string (or line, in multiline mode).\bmatches word boundaries — use it to find whole words.\Bmatches non-boundary positions (inside words).\Aand\Zare absolute string anchors unaffected by multiline mode.- Always use
^...$when validating that an entire input matches a pattern.
Next up: groups — how to capture, structure, and reference parts of your match.