Regex Special Characters You Must Escape
Some characters mean something specific to the regex engine. If you want to match them literally — not as pattern syntax — you must escape them with a backslash.
The 12 metacharacters
These characters have special meaning in most regex flavors:
. * + ? ^ $ { } [ ] ( ) | \
| Character | Role in regex |
|---|---|
. | Match any character (except newline by default) |
* | Zero or more of the preceding |
+ | One or more of the preceding |
? | Zero or one; also makes quantifiers lazy |
^ | Start of string (or line with m flag) |
$ | End of string (or line with m flag) |
{ } | Quantifier range: {2,5} means 2 to 5 |
[ ] | Character class: [a-z] |
( ) | Capture group |
| | Alternation (OR) |
\ | Escape the next character |
How to escape them
Prefix the character with \ to match it literally:
// Match a period, not "any character"
/3\.14/.test("3.14") // true
/3\.14/.test("3X14") // false
// Match a literal dollar sign
/\$100/.test("$100") // true
// Match a literal backslash
/C:\\Users/.test("C:\\Users") // true
Common mistake: URLs and file paths
URLs and paths are full of metacharacters. A naive pattern silently matches more than you expect:
// ❌ The dot matches any character
/example.com/.test("exampleXcom") // true — probably not what you wanted
// ✅ Escape the dot
/example\.com/.test("exampleXcom") // false
In JavaScript string literals, escape twice
When building a regex from a string with new RegExp(), each backslash needs to be doubled — once for the string, once for the regex engine:
const pattern = new RegExp("\\$\\d+"); // matches "$42"
// Equivalent regex literal:
const re = /\$\d+/;
With a regex literal (/.../), one backslash is enough. With a string, you need two.
Test escaping live
Paste your pattern into the regex tester and see exactly which parts of your input match as you type. If a . is matching characters it shouldn't, you're missing the backslash.
For flag modifiers like g, i, and m, see regex flags explained. For a broader reference of useful patterns, see the regex cheat sheet.
Got a config file to check?
Open the config toolkit →