← Regex Tester Regex Cheat Sheet: Quick Reference for Regular Expressions
Interactive regex cheat sheet with syntax tables for metacharacters, quantifiers, anchors, groups, lookaheads, and flags. Click any example to test it live.
100% client-side. Your data never leaves your browser.
Characters
| Pattern | Matches | Example match |
|---|
. | Any character except newline | a.c matches abc, a1c, a-c |
\d | Any digit [0-9] | \d{3} matches 123 |
\D | Any nondigit | \D+ matches abc in abc123 |
\w | Word character [a-zA-Z0-9_] | \w+ matches hello_world |
\W | Nonword character | \W matches @ in user@host |
\s | Whitespace (space, tab, newline) | \s+ matches spaces between words |
\S | Nonwhitespace | \S+ matches hello |
\t | Tab character | |
\n | Newline | |
Quantifiers
| Pattern | Meaning | Example |
|---|
* | 0 or more (greedy) | ab*c matches ac, abc, abbc |
+ | 1 or more (greedy) | ab+c matches abc, abbc but not ac |
? | 0 or 1 (optional) | colou?r matches color and colour |
{3} | Exactly 3 | \d{3} matches 123 but not 12 |
{2,5} | Between 2 and 5 | \w{2,5} matches ab through abcde |
{3,} | 3 or more | \d{3,} matches 123, 1234, 12345 |
*? | 0 or more (lazy) | ".*?" matches shortest quoted string |
+? | 1 or more (lazy) | <.+?> matches a single HTML tag |
Anchors
| Pattern | Matches | Example |
|---|
^ | Start of string (or line with m flag) | ^Hello matches Hello world |
$ | End of string (or line with m flag) | world$ matches Hello world |
\b | Word boundary | \bcat\b matches cat but not cats or concatenate |
\B | Nonword boundary | \Bcat\B matches concatenate but not cat |
Groups and Alternation
| Pattern | Meaning | Example |
|---|
(abc) | Capturing group | (ha)+ matches haha, captures ha |
(?:abc) | Noncapturing group | (?:ha)+ matches haha, no capture |
(?<name>abc) | Named capture group | (?<year>\d{4}) captures year by name |
a|b | Alternation (or) | cat|dog matches cat or dog |
\1 | Back reference to group 1 | (\w+)\s\1 matches the the |
Character Classes
| Pattern | Matches | Example |
|---|
[abc] | Any of a, b, or c | [aeiou] matches any vowel |
[^abc] | Not a, b, or c | [^0-9] matches any nondigit |
[a-z] | Range: a through z | [a-zA-Z] matches any letter |
[a-zA-Z0-9] | Alphanumeric | Same as \w minus underscore |
Lookaheads and Lookbehinds
Zero width assertions: they check a condition without consuming characters.
| Pattern | Type | Meaning | Example |
|---|
(?=foo) | Positive lookahead | Followed by foo | \d+(?= USD) matches 100 in 100 USD |
(?!foo) | Negative lookahead | NOT followed by foo | \d+(?! USD) matches 200 in 200 EUR |
(?<=foo) | Positive lookbehind | Preceded by foo | (?<=\$)\d+ matches 50 in $50 |
(?<!foo) | Negative lookbehind | NOT preceded by foo | (?<!\$)\d+ matches 50 in EUR 50 |
Lookbehinds must be fixed length in most engines. JavaScript added variable length lookbehinds in ES2018, but Safari support lagged until 2023.
Flags
| Flag | Name | Effect |
|---|
g | Global | Find all matches, not just the first |
i | Case insensitive | a matches A and a |
m | Multiline | ^ and $ match line start/end, not just string start/end |
s | Dotall | . matches newline characters too |
u | Unicode | Enable full Unicode matching and proper surrogate pair handling |
Common Patterns
These cover the majority of real world validation needs. All patterns are simplified for readability. Production systems should use dedicated parsers for complex formats like email (RFC 5322).
Email (simplified)
[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}
URL
https?://[^\s/$.?#].[^\s]*
IPv4 address
\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b
Does not validate that octets are 0-255. For strict validation, use: \b(25[0-5]|2[0-4]\d|[01]?\d\d?)(\.(25[0-5]|2[0-4]\d|[01]?\d\d?)){3}\b
Date (YYYY-MM-DD)
\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])
Hex color
#([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})\b
Phone number (US, flexible)
\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}
Matches (555) 123-4567, 555-123-4567, 555.123.4567, and 5551234567.
Greedy vs Lazy
The most common regex surprise. Given the input <b>bold</b> and <i>italic</i>:
| Pattern | Match | Why |
|---|
<.+> | <b>bold</b> and <i>italic</i> | Greedy: matches from first < to last > |
<.+?> | <b>, </b>, <i>, </i> | Lazy: matches from each < to the nearest > |
Default behavior is greedy. Append ? to any quantifier to make it lazy.
JavaScript-Specific Notes
- Use
/pattern/flags syntax or new RegExp('pattern', 'flags')
string.match(/regex/g) returns all matches as an array
string.matchAll(/regex/g) returns an iterator with capture group details
string.replace(/regex/g, replacement) replaces all matches
- Named groups:
(?<name>...) accessed via match.groups.name
\d, \w, \s are ASCII-only by default. Use the u flag for Unicode