All cheat sheets
Cheat Sheet

Regex

Regular expression quick reference: character classes, anchors, quantifiers, groups, lookahead/lookbehind, flags, and common patterns.

A regular expression (regex) is a pattern used to match text. This reference covers the syntax common to JavaScript, Python, and PCRE. Differences between flavors are noted where they exist.

Character Classes

PatternMatches
.Any character except newline (use /s flag to include newline)
\dDigit [0-9]
\DNon-digit
\wWord character [a-zA-Z0-9_]
\WNon-word character
\sWhitespace (space, tab, newline, etc.)
\SNon-whitespace
[abc]Character set — matches a, b, or c
[^abc]Negated set — matches anything except a, b, or c
[a-z]Range — matches any lowercase letter
[a-zA-Z]Range — any letter (case insensitive set)

Anchors & Boundaries

^Start of string (or line in multiline mode)
$End of string (or line in multiline mode)
\bWord boundary — between \w and \W
\BNon-word boundary
\AStart of string (Python/PCRE — not affected by multiline)
\ZEnd of string (Python/PCRE)

Quantifiers

QuantifierMeaningLazy version
*0 or more*?
+1 or more+?
?0 or 1??
{n}Exactly n times{n}?
{n,}n or more times{n,}?
{n,m}Between n and m times (inclusive){n,m}?

Greedy quantifiers match as much as possible. Lazy (? suffix) match as little as possible.

Groups & References

(abc)Capturing group — stores match in $1 / \1
(?:abc)Non-capturing group — groups without storing
(?<name>abc)Named capturing group — access as $<name> or \k<name>
\1Backreference to group 1 (within the pattern)
$1Group 1 in replacement strings
a|bAlternation — matches a or b

Lookahead & Lookbehind

(?=...)

Positive lookahead

Matches position followed by the pattern. E.g. \d+(?= dollars) matches a number before " dollars".

(?!...)

Negative lookahead

Matches position NOT followed by the pattern.

(?<=...)

Positive lookbehind

Matches position preceded by the pattern.

(?<!...)

Negative lookbehind

Matches position NOT preceded by the pattern.

Lookbehind requires fixed-width patterns in most engines. Variable-width lookbehind is supported in JS (v8), Python 3.11+, and PCRE2.

Flags / Modifiers

FlagNameEffectJSPython
iCase insensitiveMatches upper and lowercase as equal re.I
gGlobalFind all matches, not just the first findall()
mMultiline^ and $ match line start/end (not just string) re.M
sDot all. matches newline characters too re.S
uUnicodeEnables full Unicode matching default
xVerboseAllows whitespace and comments inside the regex re.X
dIndicesProvides indices for match start/end ES2022

Common Patterns

Use casePattern
Email address (basic)
/^[^\s@]+@[^\s@]+\.[^\s@]+$/i
URL (http/https)
/https?:\/\/[^\s]+/
IPv4 address
/^(\d{1,3}\.){3}\d{1,3}$/
Phone (US, flexible)
/^[+]?[1]?[\s-]?\(?\d{3}\)?[\s-]?\d{3}[\s-]?\d{4}$/
Date YYYY-MM-DD
/^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/
Hex color
/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/
UUID v4
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
Slug (URL-friendly)
/^[a-z0-9]+(?:-[a-z0-9]+)*$/
Integer (positive/negative)
/^-?\d+$/
Float
/^-?\d+(\.\d+)?$/
Whitespace-only string
/^\s*$/
Strip HTML tags
/<[^>]*>/g

Test your regex with our Regex Tester →

Real-time matching with capture group highlighting.