Skip to main content
Regexflux
Lesson 2 of 120 completed

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/g

Matches "3.14" exactly — without escaping, 3.14 would also match "3X14"

Pi is 3.14
approximately 3.14159
3X14
3.1
/\$5\.99/g

Matches the price "$5.99" literally

Price: $5.99
only $5.99 today
USD 5.99
$5X99

Exercise

Write a pattern to match the domain "www.example.com" exactly — the dots must match literal periods, not any character.

Your pattern:

Must match

Visit www.example.com today
http://www.example.com/page
see www.example.com for details

Must not match

wwwXexampleYcom
www-example-com
example.com
www.example.org