Regex Cheat Sheet
Regular expressions describe text patterns. Try any pattern below in the regex tester. Syntax differs slightly between languages, and this sheet follows JavaScript.
Characters
| Pattern | Matches |
|---|---|
. | Any character except a newline |
\d | A digit, 0 to 9 |
\w | A letter, digit or underscore |
\s | A space, tab or newline |
\D \W \S | The opposite of \d, \w, \s |
[abc] | One of a, b or c |
[a-z] | One lowercase letter |
[^abc] | Any character except a, b or c |
\. | A literal dot (escape special characters with a backslash) |
Anchors
| Pattern | Matches |
|---|---|
^ | The start of the text (or line with the m flag) |
$ | The end of the text (or line with the m flag) |
\b | A word boundary |
Quantifiers
| Pattern | Meaning |
|---|---|
* | Zero or more |
+ | One or more |
? | Zero or one |
{3} | Exactly 3 |
{2,5} | Between 2 and 5 |
{2,} | 2 or more |
+? *? | Lazy versions that match as little as possible |
Groups and choices
| Pattern | Meaning |
|---|---|
(abc) | Capturing group |
(?:abc) | Non-capturing group |
(?<year>\d{4}) | Named group |
a|b | a or b |
\1 | Repeats the first captured group |
Lookarounds
| Pattern | Meaning |
|---|---|
a(?=b) | a only if followed by b |
a(?!b) | a only if not followed by b |
(?<=b)a | a only if preceded by b |
(?<!b)a | a only if not preceded by b |
Flags
g finds all matches, i ignores case, m makes ^ and $ work per line, s lets . match newlines and u enables full Unicode.
Common patterns
- Indian PIN code:
^[1-9]\d{5}$ - Simple email check:
^[^\s@]+@[^\s@]+\.[^\s@]+$ - Date like 2026-11-02:
^\d{4}-\d{2}-\d{2}$ - Hex colour:
^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$ - Extra spaces:
\s{2,}
An email regex can only check the shape of an address. The only real test is to send a message to it.