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
  • 2 matchesfoo baz
  • 1 matchYour food
  • 1 matchBehind bars

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
  • 1 matchTry foo
  • 1 matchTry bar
  • 1 matchTry baz
  • 1 matchTry food

Try followed by one of foo, bar, and baz


Matching numbers between 100 and 250:

/1\d\d|2[0-4]\d|250/g
  • 3 matches100, 157, 199
  • 2 matches139 + 140 = 279
  • 1 match201 INR
  • 1 match$220
  • 1 match250
  • 1 match1250
  • 2 matchese = 2.71828182...
  • 0 matches251
  • 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
  • 1 match#AE25AE
  • 1 match#663399
  • 1 matchHow about #73FA79?
  • 1 matchPart of #73FA79BAC too
  • 1 match#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
  • 1 match#AE25AE
  • 1 match#663399
  • 1 matchHow about #73FA79?
  • 1 matchPart of #73FA79BAC too
  • 1 match#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
  • 1 matchMMXX
  • 1 matchVI
  • 1 matchXX
  • 1 matchXI
  • 0 matchesIXI
  • 0 matchesVV