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[RegExr] [Visual]
  • 1 matchphotoshop
    1. p
  • 1 matchpineapple
    1. p
  • 0 matchestap
    1. 0 matchesapple
      1. 1 matchppap
        1. p
      2. 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[RegExr] [Visual]
        • 1 matchphotoshop
          1. p
        • 0 matchespineapple
          1. 0 matchesapple
            1. 1 matchapp
              1. p
            2. 0 matchesPlum
              1. 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[RegExr] [Visual]
                • 1 matchp
                  1. p
                • 0 matchespi
                  1. 0 matchespea
                    1. 0 matchestarp
                      1. 0 matchesapple

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

                        /^https?$/g[RegExr] [Visual]
                        • 1 matchhttp
                          1. http
                        • 1 matchhttps
                          1. https
                        • 0 matcheshttp/2
                          1. 0 matchesshttp
                            1. 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[RegExr] [Visual]
                              • 1 matchpeach
                                1. p
                              • 1 matchbanana, peach
                                1. p
                              • 1 matchbanana+peach
                                1. p
                              • 1 matchbanana-peach
                                1. p
                              • 0 matchesbanana_peach
                                1. 0 matchesbanana%20peach
                                  1. 0 matchesgrape
                                    Note

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

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

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

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

                                                /\Bp/g[RegExr] [Visual]
                                                • 1 matchape
                                                  1. p
                                                • 1 matchleap
                                                  1. p
                                                • 1 match(leap)
                                                  1. p
                                                • 0 matchesa pot
                                                  1. 0 matchespea
                                                    /\Bp\B/g[RegExr] [Visual]
                                                    • 1 matchape
                                                      1. p
                                                    • 1 match_peel
                                                      1. p
                                                    • 0 matchesleap
                                                      1. 0 matches(leap)
                                                        1. 0 matchesa pot
                                                          1. 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[RegExr] [Visual]
                                                            • 1 matchabc
                                                            • 1 matchdef
                                                            • 0 matchesabc def

                                                              Markdown headings

                                                              /^## /gm[RegExr] [Visual]
                                                              • 0 matches# Heading 1
                                                                1. 1 match## Heading 2
                                                                  1. ##
                                                                2. 0 matches### Heading 3
                                                                  1. 0 matches#### Heading 4

                                                                    Without anchors:

                                                                    /## /gm[RegExr] [Visual]
                                                                    • 0 matches# Heading 1
                                                                      1. 1 match## Heading 2
                                                                        1. ##
                                                                      2. 1 match### Heading 3
                                                                        1. ##
                                                                      3. 1 match#### Heading 4
                                                                        1. ##