Special Characters in Regexes
Characters such as ^, $ etc have a special significance in regex patterns. In order to use such characters in their literal sense we escape them by preceding them with a reverse solidus, \. For instance the pattern Only \$\d{1,2}.\d{2} could be used to search for prices quoted in dollars. Other characters that may require such notation include ^,\,?,/,. etc. Note the need to escape the . character. A failure to realize this is at the root of many of the problems encountered when using regexes. In addition we use a special notation to represent the following non-printable characters:
| Escaped Notation | Meaning |
| \f | Form Feed |
| \n | New Line |
| \r | Carriage Return |
| \t | Tab |
| \v | Vertical Tab |
ASCII characters can be represented as \xnn where nn is the code for the character in hexadecimal. One can also use the Unicode format \unnnn - utterly pointless since only ASCII characters can be specified in this way.
Note:The characters ^$.?*!+:=()[]{}|\ do not always have to be escaped. However, doing so does make for a safer regular expression - one that is less liable to fail with less robust implementations of the regex engine. The color-coded regex editor on this site requires these characters to be escaped - except when they occur inside a regex class specification.
Do not wrap the model expression in a /.../ pair. The characters
^$.?*!+:=()[]{}|\\ must be escaped - except when then occur inside a character class. Invalid characters will be grayed out.| Result | Left Text | Match | Right Text |
|---|---|---|---|
Download