Level 2: Practical patterns · Lesson 14 of 30
Escaping special characters
A backslash turns a metacharacter into a literal. Learn which characters need escaping, and how string escaping in your language adds a second layer.
Metacharacters such as ., *, +, ?, (, ), [, ], {, }, ^, $, | and \ have special meanings. When you want to match one of them literally, put a backslash in front of it. \. matches a real dot, \$ a real dollar sign, \( a real opening parenthesis, and \\ a real backslash.
Forgetting to escape is a quiet bug. file.txt seems to work, because it matches file.txt, but it also matches filextxt, since the unescaped dot accepts any character. Writing file\.txt makes the pattern say exactly what you mean. Prices are similar: \$\d+\.\d\d matches $19.99 but not $19x99.
Some unescaped metacharacters cause errors instead. In JavaScript, C++ throws an error, because the second plus has nothing valid to repeat. Java and PHP read C++ as a possessive quantifier, which is legal but still not a literal plus. The correct pattern for the text C++ is C\+\+ everywhere.
Inside a character set fewer characters need escaping. [.$+*?()] treats all of these as literals. Inside brackets you mainly need to watch ], \, ^ at the start and - between two characters. Escaping them anyway, as in [\]\-], is always allowed.
There is often a second layer of escaping: your programming language's strings. In JavaScript, Java, C# regular strings, Go interpreted strings and PHP double-quoted strings, the backslash is also a string escape. To put the pattern \d+\.\d+ into a JavaScript string you write "\\d+\\.\\d+". Regex literals like /\d+\.\d+/ in JavaScript and Ruby avoid this.
Most languages offer raw strings to skip the second layer. Python has r"\d+", C# has verbatim strings @"\d+", Go has backtick strings, and Rust has r"\d+". Java added text blocks, but those still process backslash escapes, so Java patterns usually need doubled backslashes.
When you build a pattern from user input, escape it with a library helper. Python has re.escape, PHP has preg_quote, Java has Pattern.quote, Go has regexp.QuoteMeta, C# has Regex.Escape, Rust has regex::escape and Ruby has Regexp.escape. JavaScript recently gained RegExp.escape in newer engines, and before that developers replaced special characters with a small function.
Live tester
2 matches (JavaScript engine)
Price $19.99 or $5.00, not $7x00.
Examples
Numbers
/\d+\.\d+/g3.14 and 2x5 and 10.5
Matches: “3.14”, “10.5”
The escaped dot only accepts a real decimal point, so 2x5 is skipped.
Text
/\(\w+\)/gcall (home) or (work)
Matches: “(home)”, “(work)”
Escaped parentheses match the literal brackets instead of creating a group.
Codes
/v\d+\.\d+\.\d+/gv1.2.3 and v10.0.1 but not v1x2x3
Matches: “v1.2.3”, “v10.0.1”
Each dot in the version number is escaped so only real dots count.
Practice tasks
Your regex runs over the text with the global flag. It passes when it finds every required match and nothing that should be left out.
Task 1: Match the file name file.txt with a real dot only.
file.txt filextxt file_txt
Task 2: Match dollar prices, with optional cents.
Cost $5, $12.50, 7 dollars, 30
Task 3: Match the language name C++ exactly.
C++ C# C Cx