Literals and Escaping Special Characters
Some characters have special meanings in regex. Learn how to match them literally with backslash escaping.· 5 min
Concept
Most characters match themselves, but twelve characters have special regex meanings and must be escaped with a backslash \ when you want to match them literally:
. * + ? ^ $ { } [ ] ( ) \ |
For example:
- . matches any character, but \. matches a literal period
- $ marks end of string, but \$ matches a literal dollar sign
- ( starts a group, but \( matches a literal parenthesis
Always escape these characters when you want an exact literal match.
/3\.14/gMatches "3.14" exactly — without escaping, 3.14 would also match "3X14"
/\$5\.99/gMatches the price "$5.99" literally
Exercise
Write a pattern to match the domain "www.example.com" exactly — the dots must match literal periods, not any character.
Your pattern: