Regular Expressions For Regular Folk

Alternation

Alternation allows matching one of several phrases. This is more powerful than character classes, which are limited to characters.

Delimit the set of phrases with pipes—|.

/foo|bar|baz/g[RegExr] [Visual]
  • 2 matchesfoo baz
    1. foo
    2. baz
  • 1 matchYour food
    1. foo
  • 1 matchBehind bars
    1. bar

One of foo, bar, and baz


If only a part of the regex is to be “alternated”, wrap that part with a group—capturing or non-capturing.

/Try (foo|bar|baz)/g[RegExr] [Visual]
  • 1 matchTry foo
    1. Try foo
  • 1 matchTry bar
    1. Try bar
  • 1 matchTry baz
    1. Try baz
  • 1 matchTry food
    1. Try foo

Try followed by one of foo, bar, and baz


Matching numbers between 100 and 250:

/1\d\d|2[0-4]\d|250/g[RegExr] [Visual]
  • 3 matches100, 157, 199
    1. 100
    2. 157
    3. 199
  • 2 matches139 + 140 = 279
    1. 139
    2. 140
  • 1 match201 INR
    1. 201
  • 1 match$220
    1. 220
  • 1 match250
    1. 250
  • 1 match1250
    1. 125
  • 2 matchese = 2.71828182...
    1. 182
    2. 182
  • 0 matches251
    1. 0 matches729

      This can be generalized to match arbitrary number ranges!

      Examples

      Hex colours

      Let’s improve one of our older examples to also match shorthand hex colours.

      /#([0-9A-F]{6}|[0-9A-F]{3})/g[RegExr] [Visual]
      • 1 match#AE25AE
        1. #AE25AE
      • 1 match#663399
        1. #663399
      • 1 matchHow about #73FA79?
        1. #73FA79
      • 1 matchPart of #73FA79BAC too
        1. #73FA79
      • 1 match#FFF
        1. #FFF
      • 0 matches#a2ca2c

        It is important that [0-9A-F]{6} comes before [0-9A-F]{3}. Else:

        /#([0-9A-F]{3}|[0-9A-F]{6})/g[RegExr] [Visual]
        • 1 match#AE25AE
          1. #AE2
        • 1 match#663399
          1. #663
        • 1 matchHow about #73FA79?
          1. #73F
        • 1 matchPart of #73FA79BAC too
          1. #73F
        • 1 match#FFF
          1. #FFF
        • 0 matches#a2ca2c
          Tip

          Regex engines try alternatives from the left to the right.

          Roman numerals

          /^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$/g[RegExr] [Visual]
          • 1 matchMMXX
            1. MMXX
          • 1 matchVI
            1. VI
          • 1 matchXX
            1. XX
          • 1 matchXI
            1. XI
          • 0 matchesIXI
            1. 0 matchesVV