Anchors
Anchors do not match anything by themselves. Instead, they place restrictions on where matches may appear—“anchoring” matches.
You could also think about anchors as “invisible characters”.
Beginning of line — ^
Marked by a caret (^
) at the beginning of the regex, this anchor makes it necessary for the rest of the regex to match from the beginning of the string.
You can think of it as matching an invisible character always present at the beginning of the string.
End of line — $
This anchor is marked by a dollar ($
) at the end of the regex. It is analogous to the beginning of the line anchor.
You can think of it as matching an invisible character always present at the end of the string.
The ^
and $
anchors are often used in conjunction to ensure that the regex matches the entirety of the string, rather than merely a part.
Let’s revisit an example from Repetition, and add the two anchors at the ends of the regex.
In the absence of the anchors, http/2
and shttp
would also match.
Word boundary — \b
A word boundary is a position between a word character and a non-word character.
The word boundary anchor, \b
, matches an imaginary invisible character that exists between consecutive word and non-word characters.
Words characters include a-z
, A-Z
, 0-9
, and _
.
There is also a non-word-boundary anchors: \B
.
As the name suggests, it matches everything apart from word boundaries.
^…$
and \b…\b
are common patterns and you will almost always need one or the other to prevent accidental matches.
Examples
Trailing whitespace
Markdown headings
Without anchors: