Regex Anchors – A Complete Guide

What is a regex anchor?

A regex anchor ensures that a matched expression is anchored to a certain position in the string. The start-of-string anchor (^) ensures it’s at the start of the string while the end-of-string anchor ($) ensures it’s at the end. A word boundary (\b) ensures it’s at the start or end of a word while a non-word boundary (\B) ensures that it’s not.

Types of Anchors

There are 4 types of anchors in Javascript regex.

1. Start of String Anchor (^Edit with Regexity)

The caret symbol (^Edit with Regexity) is used as the start-of-string anchor and ensures that a match is positioned at the start of a string, or the start of a line of the multiline flag mEdit with Regexity is enabled (see below). For example, the following expression will fail if the letters abcEdit with Regexity are not positioned at the start of the string.

/^abc/Edit with Regexity

If the input string abcdefEdit with Regexity is tested against the expression above, then the abcEdit with Regexity portion of that string will be matched, since it occurs at the start of the string. However, if the input string 123abcEdit with Regexity is used then nothing will be matched.

Pro Tip: The start-of-string symbol should not be confused with the negation symbol (also the caret symbol ^Edit with Regexity) which is used at the start of a character class, e.g. [^a-z]Edit with Regexity.

2. End of String Anchor ($)

The dollar sign ($Edit with Regexity) is used as an end-of-string anchor and ensures that a match is positioned at the end of a string, or the end of a line if the multiline flag mEdit with Regexity is enabled (see below). For example, the letters abcEdit with Regexity in the next expression will only match if they are positioned at the end of a string.

/abc$/Edit with Regexity

For example, in the input string 123abcEdit with Regexity is tested against the expression above then the abcEdit with Regexity will be matched since it is positioned at the end of the string. But in the input string abcdeEdit with Regexity nothing will be matched, since abcEdit with Regexity is not positioned at the end of the string.

3. Word Boundary Anchor (\bEdit with Regexity)

The word boundary anchor (\bEdit with Regexity) will match an expression only if it occurs at the beginning of a word. Note that “words” in regex can consist of characters including [a-z]Edit with Regexity, [A-Z]Edit with Regexity, [0-9]Edit with Regexity, or underscores _Edit with Regexity.

It is helpful to think of a word boundary as an invisible character before the first character in a word and after the last character. Then it becomes easy. Place the word anchor \bEdit with Regexity in front of an expression to match it at the start of a word, or after the expression to match it at the end of a word.

For example, the expression /g\b/Edit with Regexity:

The expression /\bp/Edit with Regexity:

Pro Tip: The word boundary character \bEdit with Regexity should not be confused with the backspace character [\b]Edit with Regexity which is enclosed in square brackets and matches a backspace character.

4. Non-Word Boundary Anchor (\BEdit with Regexity)

Like many character sets in regex, the word boundary has an inverse \BEdit with Regexity that matches anything except a word boundary.

For example, using the same expression /g\b/Edit with Regexity from above:

Tips for Using Anchors

Note that placing the start-of-string anchor ^Edit with Regexity at the end of a string will result in no matches being made. This is also true for placing the end-of-string anchor $Edit with Regexity at the start of a string.

Also, using the ^Edit with Regexity and $Edit with Regexity anchors in the middle of an expression will “cancel out” either the portion before or after it. For example, in the expression abc$123Edit with Regexity the numbers 123Edit with Regexity will never match since the end-of-string anchor $Edit with Regexity signifies the end of the input string. This works vice-versa for the start of string anchor as well.

With that said, anchors work perfectly fine when used in conjunction with alternation characters |Edit with Regexity. In this case, each expression on either side of the alternation sign can have its own set of anchors. For example, the following expression will match either dogEdit with Regexity or catEdit with Regexity, each being matched both at the start and end of the string.

/^dog$|^cat$/Edit with Regexity

Using Flags with Anchors

If the multiline flag mEdit with Regexity is enabled, the start-of-string and end-of-string anchors will match the beginning and start of every line, instead of the entire input string, respectively. For example, if the expression /^u/gmEdit with Regexity is used on the following input, it will match the uEdit with Regexity at the start of every line.

until
unless
untrue

Benjamin

Founder, owner, and sole content creator on RegexLand. Enjoys programming, blogging, and teaching others how to do the same. Read more...

Leave a Comment