Regular Expressions For Regular Folk

Lookaround

Note

This section is a Work In Progress.

Lookarounds can be used to verify conditions, without matching any text.

You’re only looking, not moving.

  • Lookahead
    • Positive — (?=…)
    • Negative — (?!…)
  • Lookbehind
    • Positive — (?<=…)
    • Negative — (?<!…)

Lookahead

Positive

/_(?=[aeiou])/g[RegExr] [Visual]
  • 1 match_a
    1. _
  • 1 matche_e
    1. _
  • 0 matches_f

    Note how the character following the _ isn’t matched. Yet, its nature is confirmed by the positive lookahead.

    /(.+)_(?=[aeiou])(?=\1)/g[RegExr] [Visual]
    • 1 matche_e
      1. e_
    • 1 matchu_u
      1. u_
    • 1 matchuw_uw
      1. uw_
    • 1 matchuw_uwa
      1. uw_
    • 0 matchesf_f
      1. 0 matchesa_e

        After (?=[aeiou]), the regex engine hasn’t moved and checks for (?=\1) starting after the _.

        /(?=.*#).*/g[RegExr] [Visual]
        • 1 matchabc#def
          1. abc#def
        • 1 match#def
          1. #def
        • 1 matchabc#
          1. abc#
        • 0 matchesabcdef

          Negative

          /_(?![aeiou])/g[RegExr] [Visual]
          • 0 matches_a
            1. 0 matchese_e
              1. 1 match_f
                1. _
              /^(?!.*#).*$/g[RegExr] [Visual]
              • 0 matchesabc#def
                1. 0 matches#def
                  1. 0 matchesabc#
                    1. 1 matchabcdef
                      1. abcdef

                    Without the anchors, this will match the part without the # in each test case.


                    Negative lookaheads are commonly used to prevent particular phrases from matching.

                    /foo(?!bar)/g[RegExr] [Visual]
                    • 1 matchfoobaz
                      1. foo
                    • 0 matchesfoobarbaz
                      1. 0 matchesbazfoobar
                        /---(?:(?!---).)*---/g[RegExr] [Visual]
                        • 1 match---foo---
                          1. ---foo---
                        • 1 match---fo-o---
                          1. ---fo-o---
                        • 1 match--------
                          1. ------

                        Lookbehind

                        Limited Support

                        JavaScript, prior to ES2018, did not support this flag.

                        Positive

                        Negative

                        Examples

                        Password validation

                        /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[a-zA-Z]).{8,}$/[RegExr] [Visual]
                        • 0 matcheshunter2
                          1. 0 matcheszsofpghedake
                            1. 0 matcheszsofpghedak4e
                              1. 1 matchzSoFpghEdaK4E
                                1. zSoFpghEdaK4E
                              2. 1 matchzSoFpg!hEd!aK4E
                                1. zSoFpg!hEd!aK4E

                              Lookarounds can be used verify multiple conditions.

                              Quoted strings

                              /(['"])(?:(?!\1).)*\1/g[RegExr] [Visual]
                              • 1 matchfoo "bar" baz
                                1. "bar"
                              • 1 matchfoo 'bar' baz
                                1. 'bar'
                              • 1 matchfoo 'bat's' baz
                                1. 'bat'
                              • 1 matchfoo "bat's" baz
                                1. "bat's"
                              • 1 matchfoo 'bat"s' baz
                                1. 'bat"s'

                              Without lookaheads, this is the best we can do:

                              /(['"])[^'"]*\1/g[RegExr] [Visual]
                              • 1 matchfoo "bar" baz
                                1. "bar"
                              • 1 matchfoo 'bar' baz
                                1. 'bar'
                              • 1 matchfoo 'bat's' baz
                                1. 'bat'
                              • 0 matchesfoo "bat's" baz
                                1. 0 matchesfoo 'bat"s' baz