Regex Cheat Sheet
Last updated
Character classes
Match a single character from a set.
| Token | Matches |
|---|---|
. | Any character except a newline |
\d | Any digit (0-9) |
\D | Any non-digit |
\w | Word character (letter, digit, underscore) |
\W | Any non-word character |
\s | Any whitespace (space, tab, newline) |
[abc] | Any one of a, b, or c |
[a-z] | Any lowercase letter in the range |
[^abc] | Any character except a, b, or c |
Quantifiers
Specify how many times the preceding token repeats.
| Token | Matches |
|---|---|
* | Zero or more |
+ | One or more |
? | Zero or one (optional) |
{3} | Exactly 3 times |
{2,4} | Between 2 and 4 times |
{2,} | 2 or more times |
*? | Lazy: as few as possible |
+? | Lazy: one or more, minimal |
Anchors & boundaries
Match positions rather than characters.
| Token | Matches |
|---|---|
^ | Start of the string (or line) |
$ | End of the string (or line) |
\b | A word boundary |
\B | A non-word boundary |
^abc$ | A string that is exactly abc |
\bword\b | word as a whole word |
Groups & capturing
Group tokens and capture what they match.
| Token | Matches |
|---|---|
(abc) | Capturing group, stored as group 1 |
(?:abc) | Non-capturing group |
(?<year>\d{4}) | Named capturing group year |
(ab)+ | Repeat the whole group |
\1 | Backreference to group 1 |
\k<year> | Backreference to a named group |
Alternation
Match one pattern or another.
| Token | Matches |
|---|---|
a|b | Either a or b |
cat|dog | The word cat or dog |
(jpg|png|gif) | Any one of the alternatives, captured |
gr(a|e)y | gray or grey |
^(yes|no)$ | Exactly yes or exactly no |
Lookarounds
Assert what comes before or after without consuming it.
| Token | Matches |
|---|---|
(?=abc) | Lookahead: followed by abc |
(?!abc) | Negative lookahead: not followed by abc |
(?<=abc) | Lookbehind: preceded by abc |
(?<!abc) | Negative lookbehind: not preceded by abc |
\d+(?= dollars) | Digits only when followed by dollars |
(?<=\$)\d+ | Digits only when preceded by $ |
Flags
Modify how the whole pattern is applied.
| Flag | What it does |
|---|---|
g | Global: find all matches, not just the first |
i | Case-insensitive matching |
m | Multiline: ^ and $ match line ends |
s | Dotall: . also matches newlines |
u | Unicode mode |
x | Extended: ignore whitespace in the pattern |
Common patterns
Ready-to-adapt patterns for everyday validation.
| Pattern | Matches |
|---|---|
^\d+$ | A whole number (digits only) |
^[\w.+-]+@[\w-]+\.[\w.-]+$ | A basic email address |
https?:\/\/[^\s]+ | An http or https URL |
^\d{4}-\d{2}-\d{2}$ | A date like 2026-05-27 |
^\d{3}-\d{3}-\d{4}$ | A US phone number |
^#?[0-9a-fA-F]{6}$ | A 6-digit hex color |
\s+ | One or more whitespace characters |
Every regex token you reach for, on one page. This regex cheat sheet is a quick reference for building regular expressions - character classes, quantifiers, anchors, groups, alternation, lookarounds, and flags.
The syntax here follows the common PCRE / JavaScript flavor used by most languages. Copy a pattern, or try it live in the regex tester tool to see exactly what it matches, with the JS playground a click away.
Regex cheat sheet FAQ
Is this regex cheat sheet free?
What is the difference between greedy and lazy quantifiers?
* and + match as much as possible, then give characters back if the rest of the pattern needs them. Lazy quantifiers, written by adding ? (such as *? or +?), match as little as possible and only expand when forced. For example, on <a><b> the pattern <.+> greedily matches the whole string, while <.+?> matches just <a>.What is a capture group in regex?
(\d{4}), whose matched text is saved and numbered so you can reuse or extract it. You reference it later with a backreference such as \1, or name it with (?<year>\d{4}) for clarity. If you want to group tokens without capturing, use a non-capturing group (?:...).