The Pug Automatic

Brackets in regular expressions

Written September 23, 2012. Tagged Regular expressions.

For some reason, people often seem to confuse () and [] in regular expressions.

Say you want to match only the strings "bar" and "car".

I sometimes see this mistake:

/[b|c]ar/

It will indeed match "bar" and "car" as intended. But it will also match "|ar".

Round brackets do grouping (and capture groups, and some other things). Within the group, you can use | for alternation. So this would work as expected:

/(b|c)ar/

But square brackets are not the same as round brackets. Square brackets are syntax sugar for character-level alternation.

[abcd] effectively expands to (a|b|c|d).

So if you do [b|c], that's syntax sugar for (b|\||c). Probably not what you want.

Instead do this:

/[bc]ar/