Regex Cheat Sheet — Complete Reference (PCRE, JavaScript, Python)
Complete regular expression cheat sheet with every anchor, character class, quantifier, group, and flag — plus 30+ ready-to-use patterns for emails, URLs, dates, and more.
Everything you need to write and read regular expressions, on one page. Works for JavaScript, Python, PCRE, and most modern regex engines — dialect-specific notes are called out where they matter. Try any pattern immediately in the regex tester.
Character classes
| Token | Matches |
|---|---|
. | Any character except newline (add s flag for dotall) |
\w | Word character: letters, digits, underscore |
\W | Non-word character |
\d | Digit (0-9) |
\D | Non-digit |
\s | Whitespace (space, tab, newline) |
\S | Non-whitespace |
[abc] | Any of a, b, or c |
[^abc] | Anything except a, b, or c |
[a-z] | Any lowercase letter |
[A-Z] | Any uppercase letter |
[0-9] | Any digit (same as \d) |
\p{L} | Any Unicode letter (needs u flag) |
\p{N} | Any Unicode number |
\p{Emoji} | Any emoji (Unicode-aware engines) |
Anchors & boundaries
| Token | Matches |
|---|---|
^ | Start of string (or line with m flag) |
$ | End of string (or line with m flag) |
\b | Word boundary |
\B | Non-word boundary |
\A | Start of string only (not per-line, even with m) |
\Z | End of string only |
Quantifiers
| Token | Meaning |
|---|---|
* | 0 or more (greedy) |
+ | 1 or more (greedy) |
? | 0 or 1 (optional) |
{n} | Exactly n |
{n,} | n or more |
{n,m} | Between n and m |
*? +? ?? {n,m}? | Lazy (non-greedy) versions — match as little as possible |
*+ ++ | Possessive (PCRE/Java only) — no backtracking |
Groups & references
| Syntax | Meaning |
|---|---|
(abc) | Capturing group |
(?:abc) | Non-capturing group (no backref slot) |
(?<name>abc) | Named capturing group |
\1 \2 … | Back-reference to group 1, 2, … |
\k<name> | Back-reference to named group |
$1 $2 | Reference in replacement string |
a|b | Alternation (a OR b) |
Lookarounds
| Syntax | Meaning | Example |
|---|---|---|
(?=...) | Lookahead: followed by | \d+(?= dollars) |
(?!...) | Negative lookahead | foo(?!bar) |
(?<=...) | Lookbehind: preceded by | (?<=\$)\d+ |
(?<!...) | Negative lookbehind | (?<!no )good |
Flags (modifiers)
| Flag | Effect |
|---|---|
g | Global — find all matches, not just first |
i | Case-insensitive |
m | Multi-line — ^/$ match line boundaries |
s | Dotall — . matches newlines |
u | Unicode — enables \p{...} |
y | Sticky — match from lastIndex only (JS) |
x | Extended — whitespace + comments inside pattern (PCRE, Python) |
Ready-to-use patterns
| Pattern | Regex |
|---|---|
| Email (practical) | [^\s@]+@[^\s@]+\.[^\s@]+ |
| URL (http/https) | https?:\/\/\S+ |
| IPv4 | ^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$ |
| UUID | ^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$ |
| Hex color | ^#(?:[0-9a-fA-F]{3}){1,2}$ |
| Date YYYY-MM-DD | ^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$ |
| Time HH:MM (24h) | ^([01]\d|2[0-3]):[0-5]\d$ |
| US phone | ^\+?1?[\s.-]?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$ |
| Password (8+ with mix) | ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$ |
| Positive integer | ^[1-9]\d*$ |
| Integer (with sign) | ^-?\d+$ |
| Float | ^-?\d+(\.\d+)?$ |
| Slug (kebab-case) | ^[a-z0-9]+(?:-[a-z0-9]+)*$ |
| Leading/trailing whitespace | ^\s+|\s+$ |
| Consecutive duplicates | (.)\1+ |
| HTML tag (simple) | <\/?[a-zA-Z][^>]*> |
| Credit card (generic) | ^\d{4}[ -]?\d{4}[ -]?\d{4}[ -]?\d{4}$ |
| Extract quoted string | "([^"\\]*(?:\\.[^"\\]*)*)" |
Common gotchas
- Greedy by default.
.*matches as much as possible; use.*?for the shortest match. - Escape specials:
. * + ? ( ) [ ] { } | \ ^ $ /need a backslash when literal. - Inside character classes most specials lose their meaning; only
- ] \and^(at start) need escaping. - Don't parse HTML with regex — use a parser. Regex handles flat text, not nested markup.
- Lookbehind limits — JavaScript supports variable-width lookbehind since 2018; older engines require fixed width.
- Performance trap: nested quantifiers like
(a+)+cause catastrophic backtracking. Use possessive quantifiers or rewrite the pattern.
Test any pattern
Drop any pattern from this page into the regex tester — matches highlight live, capture groups extract separately, and flags toggle from a menu. For diffing two files line-by-line (a common regex alternative), use diff checker. For side-by-side text comparison, text compare.
Featured Tools
Try these free tools directly in your browser — no sign-up required.
Regex Tester
Test and debug regular expressions in real time. Highlights matches, capture groups, and supports JavaScript regex flags for instant pattern validation.
Text Compare
Compare two blocks of text side by side and highlight the differences. Find added, removed, and changed lines instantly with color-coded diff.
Diff Checker
Compare two texts or code files side by side and highlight differences. Find added, removed, and changed lines instantly with colour-coded diff output.