Escaping special characters
These characters need a backslash to be matched literally: . * + ? ^ $ ( ) [ ] { } | \ and, in a JavaScript literal, the slash. A price like $4.99 is \$4\.99. Fewer characters are special inside a character class. Escape user input before building a pattern from it.
Syntax
\. \* \+ \? \^ \$ \( \) \[ \] \{ \} \| \\ \/Examples
"Costs $4.99 or $4x99".match(/\$4\.99/g)Result: ["$4.99"]
Without the backslashes, $ means end of string and . means any character.
"1+1=2".replace(/\+/, " plus ")Result: "1 plus 1=2"
const esc = s => s.replace(/[.*+?^{}$()|[\]\\]/g, "\\$&");
new RegExp("^" + esc("a.b*c") + "$").test("a.b*c")Result: true
Escape user input before putting it in a pattern; newer engines offer RegExp.escape().
new RegExp("\\d+\\.\\d+").test("3.14")Result: true
In a string the backslash itself must be doubled.
Common mistakes
- Leaving the dot unescaped: a.b matches axb and a b as well as a.b.
- Passing a string with single backslashes to new RegExp: "\d" becomes "d" before the regex sees it; write "\\d".
- Forgetting to escape / in a JavaScript literal, which ends the regex early and throws a SyntaxError.
- Dropping raw user input into new RegExp, so a search for "c++" throws and "." matches everything.
Practice Escaping special characters
These drills check your answer on live data, free, no account needed.
- Escaping: a price like $4.99The whole line must match. Match a dollar sign, one or more digits, a period and exactly two digits.
- Currency amounts with commasThe whole line must match. Match a dollar amount: $, then 0 or a number without leading zeros that uses a comma every three digits (so $1,234, never $1234), then optionally a period and exactly two digits of cents.
- Lookbehind: amounts after $Match every amount that comes right after a dollar sign: digits with optional two-digit cents. The whole match is compared, so it must not include the $.
Questions
How do I match a literal backslash?
Escape it: \\ in the pattern. In a JavaScript or Python string without raw syntax that becomes "\\\\" (four backslashes).
How do I match a dollar sign in regex?
\$ matches a literal dollar. Inside a character class, [$] works without the backslash.
Do I need to escape a hyphen or a slash?
A hyphen only inside a character class when it is not first or last. A slash only inside a JavaScript regex literal; in strings and in other languages it is a normal character.
Related
- Character classesWhat a character class matches, how ranges and negation work, what \d \w \s and their uppercase opposites mean, and what the dot does and does not match.
- Negated character classHow [^...] matches any character except the listed ones, stripping non-digits, grabbing quoted text, splitting on a delimiter, and how it differs from (?!).
- Regex for URLA URL regex for validation and for pulling links out of text, capturing scheme, host, port and path, avoiding trailing punctuation, and when to use new URL.
- match, test, replace in JavaScriptWhich JavaScript regex method to use for a yes/no check, the first match, all matches with groups, substitution and splitting, with return values and g rules.