Regex cheat sheet
Regular expressions describe text patterns. This cheat sheet covers the parts you use every day, in the syntax shared by JavaScript, Python, Rust and most other languages.
Regular expression cheat sheetMDN cheat sheet
Characters
.- Any character except newline
\d \D- Digit / non-digit
\w \W- Word character / non-word
\s \S- Whitespace / non-whitespace
[abc]- Any of a, b or c
[^abc]- Anything except a, b, c
[a-z]- A character in the range
Anchors
^ $- Start / end of line (or string)
\b \B- Word boundary / not a boundary
Quantifiers
*- 0 or more
+- 1 or more
?- 0 or 1
{n}- Exactly n
{n,m}- Between n and m
*? +?- Lazy: as few as possible
Groups
(abc)- Capture group
(?:abc)- Non-capturing group
(?<name>abc)- Named group
a|b- a or b
\1- Back-reference to group 1
Lookaround
(?=abc)- Followed by abc
(?!abc)- Not followed by abc
(?<=abc)- Preceded by abc
(?<!abc)- Not preceded by abc
Flags
i- Ignore case
g- Global: find all matches
m- ^ and $ match at each line
s- . also matches newline
u- Unicode mode
How to use it
- Find the group you need.
- Combine the pieces into a pattern.
- Test it against real input before shipping.
This tool is also built into Zest search, so you can type your question into the search box and get the same answer above the results.
Common questions
- What is the difference between greedy and lazy?
- Greedy quantifiers like * take as much as they can. Adding ? makes them lazy, taking as little as possible.
- What does \b mean?
- A word boundary: the position between a word character and a non-word character.