A primer on the ECMAScript regex grammar
The rules for reading and writing regexes in the ECMAScript dialect are simple. A regex is just a string of characters (such as a[bc].d*e
), and you read it from left to right. Most characters represent only themselves, so that cat
is a valid regex and matches only the literal string "cat"
. The only characters that don't represent themselves--and thus the only way to build regexes that represent languages more interesting than "cat"
--are the following punctuation characters:
^ $ \ . * + ? ( ) [ ] { } |
\
--if you're using a regex to describe a set of strings involving punctuation characters, you can use a backslash to escape those special characters. For example, \$42\.00
is a regex for the singleton language whose only member is the string "$42.00"
. Perhaps confusingly, backslash is also used to turn some normal characters into special characters! n
is a regex for the letter "n", but \n
is a regex for the newline character. d
is a regex for the letter...