Cheat sheet · Tools
Regular expressions cheat sheet
The symbols of regular expressions with plain examples, and how to use them in JavaScript.
- Sections
- 7
- Items
- 30
- Area
- Tools
- Download
- PNG, PDF
Cheat sheet
Regular expressions
The symbols of regular expressions with plain examples, and how to use them in JavaScript.
Characters
.- Any one character except a new line
\d \w \s- A digit / a letter, digit or _ / a space
\D \W \S- The opposite of each of those
[abc]- One of a, b or c
[^abc]- Any character except a, b, c
[a-z0-9]- A range of characters
\.- A real dot (a backslash switches off the special meaning)
Anchors
^ / $- Start / end of the text
\b- Edge of a word
How many
*- Zero or more
+- One or more
?- Zero or one (optional)
{3} {2,5} {3,}- Exactly 3 / 2 to 5 / 3 or more
*? +?- As few as possible (lazy)
Groups
(abc)- Group and remember the match
(?:abc)- Group without remembering
a|b- a or b
(?<year>\d{4})- A named group
\1- Repeat what group 1 matched
Look around
a(?=b)- a only if b comes next
a(?!b)- a only if b does not come next
(?<=b)a- a only if b comes before
In JavaScript
/\d+/g- Flags: g all matches, i ignore case, m per line
re.test(text)- true or false
text.match(re)- The match, or all matches with g
text.replace(re, "x")- Replace matches
[...text.matchAll(re)]- All matches with their groups
Patterns to start from
^\d{4}-\d{2}-\d{2}$- A date like 2026-10-20
^#?[0-9a-fA-F]{6}$- A hex colour like #38bdf8
^\S+@\S+\.\S+$- Rough email check (confirm by sending a mail)
Remember
- Test your pattern on a few good and a few bad examples before you trust it.
- A very nested pattern on long text can run for a long time. Keep patterns simple.
Learn it properly
A cheat sheet helps you remember. These lessons explain the ideas step by step, with examples.