Regular Expressions For Regular Folk

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.

/^p/g
  • 1 matchphotoshop
  • 1 matchpineapple
  • 0 matchestap
  • 0 matchesapple
  • 1 matchppap
  • 0 matchesmango

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.

/p$/g
  • 1 matchphotoshop
  • 0 matchespineapple
  • 0 matchesapple
  • 1 matchapp
  • 0 matchesPlum
  • 0 matchesmango

The ^ and $ anchors are often used in conjunction to ensure that the regex matches the entirety of the string, rather than merely a part.

/^p$/g
  • 1 matchp
  • 0 matchespi
  • 0 matchespea
  • 0 matchestarp
  • 0 matchesapple

Let’s revisit an example from Repetition, and add the two anchors at the ends of the regex.

/^https?$/g
  • 1 matchhttp
  • 1 matchhttps
  • 0 matcheshttp/2
  • 0 matchesshttp
  • 0 matchesftp

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.

/\bp/g
  • 1 matchpeach
  • 1 matchbanana, peach
  • 1 matchbanana+peach
  • 1 matchbanana-peach
  • 0 matchesbanana_peach
  • 0 matchesbanana%20peach
  • 0 matchesgrape
Note

Words characters include a-z, A-Z, 0-9, and _.

/\bp\b/g
  • 1 matchword p word
  • 1 match(p)
  • 1 matchp+q+r
  • 0 matches(paren)
  • 0 matches(loop)
  • 0 matchesloops
/\bcat\b/g
  • 1 matchcat
  • 1 matchthe cat?
  • 0 matchescatch
  • 0 matchesconcat it
  • 0 matchesconcatenate

There is also a non-word-boundary anchors: \B.

As the name suggests, it matches everything apart from word boundaries.

/\Bp/g
  • 1 matchape
  • 1 matchleap
  • 1 match(leap)
  • 0 matchesa pot
  • 0 matchespea
/\Bp\B/g
  • 1 matchape
  • 1 match_peel
  • 0 matchesleap
  • 0 matches(leap)
  • 0 matchesa pot
  • 0 matchespea
Tip

^…$ and \b…\b are common patterns and you will almost always need one or the other to prevent accidental matches.

Examples

Trailing whitespace

/\s+$/gm
  • 1 matchabc
  • 1 matchdef
  • 0 matchesabc def

Markdown headings

/^## /gm
  • 0 matches# Heading 1
  • 1 match## Heading 2
  • 0 matches### Heading 3
  • 0 matches#### Heading 4

Without anchors:

/## /gm
  • 0 matches# Heading 1
  • 1 match## Heading 2
  • 1 match### Heading 3
  • 1 match#### Heading 4