There are a number of metacharacters that require special treatment when trying to match them. To match these special characters, you must first escape those characters, that is, precede them with a backslash character (\).
Special Characters and Associated Meanings
Special Character | Comment |
---|---|
$ |
Matches the position at the end of an input string. If the RegExp object's Multiline property is set, $ also matches the position preceding '\n' or '\r'. To match the $ character itself, use \$. |
( ) |
Marks the beginning and end of a subexpression. Subexpressions can be captured for later use. To match these characters, use \( and \). |
* |
Matches the preceding character or subexpression zero or more times. To match the * character, use \*. |
+ |
Matches the preceding character or subexpression one or more times. To match the + character, use \+. |
. |
Matches any single character except the newline character \n. To match ., use \. |
[ ] |
Marks the beginning of a bracket expression. To match these characters, use \[ and \]. |
? |
Matches the preceding character or subexpression zero or one time, or indicates a non-greedy quantifier. To match the ? character, use \?. |
\ |
Marks the next character as either a special character, a literal, a backreference, or an octal escape. For example, 'n' matches the character 'n'. '\n' matches a newline character. The sequence '\\' matches "\" and '\(' matches "(". |
/ |
Denotes the start or end of a literal regular expression. To match the '/' character, use '\/'. |
^ |
Matches the position at the beginning of an input string except when used in a bracket expression where it negates the character set. To match the ^ character itself, use \^. |
{ } |
Marks the beginning of a quantifier expression. To match these characters, use \{ and \}. |
| |
Indicates a choice between two items. To match |, use \|. |