Regex · Reference
Regex Cheat Sheet — Complete Reference
Regular expressions are one of the most powerful tools in a developer's toolkit — and one of the hardest to remember. This cheat sheet covers every pattern you'll actually use, with real examples.
Free Online Regex Tester
Test and debug regular expressions with live match highlighting. Runs entirely in your browser.
Open Regex Tester →Anchors
Anchors don't match characters — they match positions in a string.
| Pattern | Matches | Example |
|---|---|---|
^ | Start of string | ^Hello matches "Hello world" |
$ | End of string | world$ matches "Hello world" |
\b | Word boundary | \bcat\b matches "cat" not "catfish" |
\B | Not a word boundary | \Bcat\B matches "concatenate" |
Character classes
| Pattern | Matches |
|---|---|
. | Any character except newline |
\d | Any digit [0-9] |
\D | Any non-digit |
\w | Word character [a-zA-Z0-9_] |
\W | Non-word character |
\s | Whitespace (space, tab, newline) |
\S | Non-whitespace |
[abc] | Any of a, b, or c |
[^abc] | Not a, b, or c |
[a-z] | Any lowercase letter |
[a-zA-Z0-9] | Any alphanumeric character |
Quantifiers
| Pattern | Matches |
|---|---|
* | 0 or more times |
+ | 1 or more times |
? | 0 or 1 time (optional) |
{3} | Exactly 3 times |
{3,} | 3 or more times |
{3,6} | Between 3 and 6 times |
*? | 0 or more (lazy — as few as possible) |
+? | 1 or more (lazy) |
Groups and references
| Pattern | Meaning |
|---|---|
(abc) | Capture group |
(?:abc) | Non-capturing group |
(?<name>abc) | Named capture group |
\1 | Backreference to group 1 |
a|b | Match a or b |
Lookahead and lookbehind
| Pattern | Meaning |
|---|---|
(?=abc) | Positive lookahead — followed by abc |
(?!abc) | Negative lookahead — not followed by abc |
(?<=abc) | Positive lookbehind — preceded by abc |
(?<!abc) | Negative lookbehind — not preceded by abc |
Common regex patterns
Copy-paste ready patterns for the most common validation tasks:
Email address
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$URL (http/https)
https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&/=]*)IP address (IPv4)
^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$Phone number (flexible)
^[+]?[(]?[0-9]{3}[)]?[-\s.]?[0-9]{3}[-\s.]?[0-9]{4,6}$Hex color code
^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$Strong password (8+ chars, upper, lower, number, symbol)
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$Slug (URL-safe string)
^[a-z0-9]+(?:-[a-z0-9]+)*$
UUID v4
^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$Regex flags
| Flag | Meaning |
|---|---|
g | Global — find all matches, not just the first |
i | Case-insensitive |
m | Multiline — ^ and $ match start/end of each line |
s | Dotall — . matches newlines too |
u | Unicode — enables full Unicode matching |
Pro tip: Test every regex before deploying it. Use the tinybench.dev regex tester to paste your pattern, see matches highlighted in real time, and inspect capture groups. Saves hours of debugging.
Frequently asked questions
What is the difference between greedy and lazy quantifiers?
Greedy quantifiers (*, +, {n,}) match as much as possible. Lazy quantifiers (*?, +?, {n,}?) match as little as possible. For example, given "aXbXc", the pattern a.*c greedily matches the entire string, while a.*?c matches up to the first c.
Are regex patterns the same in every language?
Mostly, but not entirely. JavaScript, Python, Java, and PHP all use similar syntax but have subtle differences in lookbehind support, Unicode handling, and available flags. Always test in the language you're targeting.
How do I match a literal dot or special character?
Escape it with a backslash: \. matches a literal dot, \* matches a literal asterisk. Special characters that need escaping: . * + ? ^ $ { } [ ] | ( ) \
Try it now — free & private
Runs entirely in your browser. No sign-up, no uploads, no tracking.
Open Regex Tester →