Interactive regex cheat sheet with syntax tables for metacharacters, quantifiers, anchors, groups, lookaheads, and flags. Click any example to test it live.
sales@company.co.ukindex 37| 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 |
| 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 |
| 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 |
| 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 |
| 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 |
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.
| 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 |
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).
[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}
https?://[^\s/$.?#].[^\s]*
\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
\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])
#([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})\b
\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}
Matches (555) 123-4567, 555-123-4567, 555.123.4567, and 5551234567.
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.
/pattern/flags syntax or new RegExp('pattern', 'flags')string.match(/regex/g) returns all matches as an arraystring.matchAll(/regex/g) returns an iterator with capture group detailsstring.replace(/regex/g, replacement) replaces all matches(?<name>...) accessed via match.groups.name\d, \w, \s are ASCII-only by default. Use the u flag for UnicodeNothing you paste leaves this tab. Every tool runs entirely in your browser — no upload, no server, no account.