A compact regex cheat sheet with the most-used patterns. For a step-by-step tutorial, see Regular Expressions Cheat Sheet with Examples. To test patterns interactively, use our free Regex Tester.
Character Classes
| Pattern | Description | Example | Matches |
|---|
. | Any character except newline | a.c | abc, a1c, a-c |
\d | Digit (0โ9) | \d{3} | 123, 456 |
\D | Non-digit | \D+ | abc, --- |
\w | Word character (aโz, AโZ, 0โ9, _) | \w+ | hello, var_1 |
\W | Non-word character | \W | @, #, space |
\s | Whitespace (space, tab, newline) | a\sb | a b |
\S | Non-whitespace | \S+ | hello |
[abc] | Any of a, b, or c | [aeiou] | a, e, i |
[^abc] | Not a, b, or c | [^0-9] | a, Z, - |
[a-z] | Range: a through z | [A-Za-z] | a, Z |
Quantifiers
| Pattern | Description | Example | Matches |
|---|
* | 0 or more | ab*c | ac, abc, abbc |
+ | 1 or more | ab+c | abc, abbc |
? | 0 or 1 | colou?r | color, colour |
{n} | Exactly n | \d{4} | 2026 |
{n,} | n or more | \d{2,} | 12, 123, 1234 |
{n,m} | Between n and m | \d{2,4} | 12, 123, 1234 |
*? | Lazy * (as few as possible) | ".*?" | โaโ in โaโ โbโ |
+? | Lazy + | ".+?" | โaโ in โaโ โbโ |
Anchors
| Pattern | Description | Example |
|---|
^ | Start of string (or line with m flag) | ^Hello |
$ | End of string (or line with m flag) | world$ |
\b | Word boundary | \bword\b |
\B | Non-word boundary | \Bend |
Groups and References
| Pattern | Description | Example | Matches |
|---|
(abc) | Capturing group | (ha)+ | ha, haha |
(?:abc) | Non-capturing group | (?:ha)+ | ha, haha |
(?<name>abc) | Named group | (?<year>\d{4}) | 2026 |
\1 | Back-reference to group 1 | (a)\1 | aa |
(?P=name) | Named back-reference (Python) | โ | โ |
Lookaround
| Pattern | Description | Example | Matches |
|---|
(?=abc) | Lookahead: followed by abc | \d(?=px) | 5 in 5px |
(?!abc) | Negative lookahead: not followed by abc | \d(?!px) | 5 in 5em |
(?<=abc) | Lookbehind: preceded by abc | (?<=\$)\d+ | 100 in $100 |
(?<!abc) | Negative lookbehind: not preceded by abc | (?<!\$)\d+ | 100 in โฌ100 |
Flags
| Flag | Name | Description |
|---|
g | Global | Find all matches, not just the first |
i | Case-insensitive | A matches a |
m | Multiline | ^ and $ match line boundaries |
s | DotAll | . matches newlines too |
u | Unicode | Enable Unicode matching |
x | Extended | Ignore whitespace, allow comments |
Common Patterns
| Purpose | Pattern |
|---|
| Email (simple) | [\w.-]+@[\w.-]+\.\w{2,} |
| URL | https?://[^\s/$.?#].[^\s]* |
| IPv4 address | \b\d{1,3}(\.\d{1,3}){3}\b |
| Hex color | #[0-9a-fA-F]{3,8} |
| ISO date (YYYY-MM-DD) | \d{4}-\d{2}-\d{2} |
| Phone (US) | \(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4} |
| HTML tag | <([a-z][\w-]*)\b[^>]*>.*?</\1> |
| Integer | -?\d+ |
| Float | -?\d+\.\d+ |
| Slug | [a-z0-9]+(?:-[a-z0-9]+)* |
Special Characters (Need Escaping)
These characters have special meaning and must be escaped with \ to match literally:
. * + ? ^ $ | \ ( ) [ ] { }
For example, to match a literal dot: \.
To match a literal dollar sign: \$
Quick Tips
- Test before you commit: open the Regex Tester in a new tab and verify matches against real sample text before pasting the pattern into your codebase.
- Use anchors for full-string validation: wrap your pattern in
^...$ so it only matches when the entire string conforms.
- Prefer character classes over
.: [^"]+ is faster and clearer than .+? when matching content between delimiters.
- Watch out for catastrophic backtracking: nested quantifiers like
(a+)+ can hang on non-matching input. Avoid them or use atomic groups where supported.
- Different engines, different syntax: JavaScript, Python, Java, and POSIX regex have subtle differences. The patterns here target JavaScript / PCRE-compatible engines.