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.

PatternMatchesExample
^Start of string^Hello matches "Hello world"
$End of stringworld$ matches "Hello world"
\bWord boundary\bcat\b matches "cat" not "catfish"
\BNot a word boundary\Bcat\B matches "concatenate"

Character classes

PatternMatches
.Any character except newline
\dAny digit [0-9]
\DAny non-digit
\wWord character [a-zA-Z0-9_]
\WNon-word character
\sWhitespace (space, tab, newline)
\SNon-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

PatternMatches
*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

PatternMeaning
(abc)Capture group
(?:abc)Non-capturing group
(?<name>abc)Named capture group
\1Backreference to group 1
a|bMatch a or b

Lookahead and lookbehind

PatternMeaning
(?=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

FlagMeaning
gGlobal — find all matches, not just the first
iCase-insensitive
mMultiline — ^ and $ match start/end of each line
sDotall — . matches newlines too
uUnicode — 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 →

Related tools on tinybench.dev