Stax

Regex Cheat Sheet

Quick reference for regular expressions: anchors, character classes, quantifiers, groups, lookarounds, flags, and ready-to-use patterns.

Anchors

PatternMatches
^Start of string (or line with m flag)
$End of string (or line with m flag)
\bWord boundary (between \w and non-word)
\BNon-word boundary
\A / \ZStart / end of string (PCRE only — not JS)

Character classes

PatternMatches
.Any character except newline (or any with s flag)
\dDigit [0-9]
\DNon-digit
\wWord char [A-Za-z0-9_]
\WNon-word char
\sWhitespace (space, tab, newline)
\SNon-whitespace
[abc]Any of a, b, or c
[^abc]Anything except a, b, or c
[a-z]Range a through z
[a-zA-Z0-9]Multiple ranges

Quantifiers

PatternMatches
*0 or more (greedy)
+1 or more (greedy)
?0 or 1 (optional)
{3}Exactly 3
{2,5}2 to 5
{2,}2 or more
*? +? ??Lazy (match as little as possible)
*+ ++ ?+Possessive (no backtracking — PCRE)

Groups & references

PatternMatches
(abc)Capture group 1
(?:abc)Non-capturing group
(?<name>abc)Named group (use \k<name> to backreference)
\1 \2Backreference to group 1, 2
a|ba or b (alternation)

Lookarounds

PatternMatches
(?=abc)Followed by abc (positive lookahead)
(?!abc)NOT followed by abc (negative lookahead)
(?<=abc)Preceded by abc (positive lookbehind)
(?<!abc)NOT preceded by abc (negative lookbehind)

Flags

FlagEffect
gGlobal — find all matches, not just first
iCase-insensitive
mMultiline — ^ and $ match line boundaries
sDotall — . matches newline too
uUnicode mode
ySticky (JavaScript) — match at lastIndex only

Common ready-to-use patterns

Use casePattern
Email (basic)^[\w.+-]+@[\w-]+\.[\w.-]+$
URL (http/https)^https?://[^\s/$.?#].[^\s]*$
IPv4^(\d{1,3}\.){3}\d{1,3}$
Indian phone^(\+91[\-\s]?)?[0]?(91)?[6-9]\d{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}$
ISO 8601 date^\d{4}-\d{2}-\d{2}$
Hex color^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$
Slug (URL-friendly)^[a-z0-9]+(?:-[a-z0-9]+)*$
PAN (India)^[A-Z]{5}[0-9]{4}[A-Z]$
GSTIN (India)^\d{2}[A-Z]{5}\d{4}[A-Z]\d[Z]\d$

Test any pattern interactively in our Regex Tester.

How to use this cheat sheet

Skim the section that matches your problem (anchor, quantifier, group, etc.), copy a pattern, then test it live in our Regex Tester. The patterns work in JavaScript, PCRE, and most modern regex flavors. Differences are noted where they matter.

Common pitfalls

Greedy vs lazy: .* matches as much as possible, .*? matches as little. Anchors ^/$ match line boundaries with the m flag, otherwise string boundaries. Inside character classes most metacharacters lose meaning. Always escape \\ in JavaScript string literals — "\\\\d" in a string equals \\d in the regex.

Frequently asked questions

How do I learn regex fast?
Memorize the four pillars: anchors (^ $ \b), character classes (\d \w \s . []), quantifiers (* + ? {n,m}), and groups (). Everything else builds on these. Test every pattern as you learn it — rote memorization without practice fails fast. Spend 30 minutes a day for two weeks and you'll cover 95% of real-world cases.
What's the difference between PCRE, JavaScript, and POSIX regex?
PCRE (Perl-compatible) is the richest dialect — supports lookbehind, named groups, recursion. JavaScript regex is similar to PCRE but lacks variable-length lookbehind in older browsers. POSIX is older, mostly used in grep/sed/awk; lacks lookarounds entirely. Most online regex testers default to PCRE or JavaScript flavor.
When should I NOT use regex?
Parsing HTML, XML, or JSON — use proper parsers. Matching balanced brackets/parens — regex can't count. Validating email addresses for delivery — use proper email validation libraries that check MX records. The 'if it can be parsed' rule: if the language has a defined grammar, use a parser, not regex.
Why is .* so slow on long input?
It's catastrophic backtracking. Greedy quantifiers like .* try every possible match before giving up. On long inputs with nested quantifiers (e.g., (a+)+), regex can take exponential time. Solutions: use atomic groups (?>...), possessive quantifiers (*+, ++), or anchor with lookarounds. Re-write to avoid nested optionality.
How do I match a literal special character?
Escape it with a backslash. Special chars in regex: . * + ? ^ $ ( ) [ ] { } | \ /. To match a literal dot: \. To match a literal backslash: \\. Inside character classes [] most special chars lose meaning except - ] \ ^. So [.] matches a literal dot without escaping needed inside the class.

Related tools