Regex cheat sheet.

Every token, character class, group construct, quantifier, anchor, and flag. Designed for printing — pin it to your wall, never Google "regex word boundary" again.

Character classes

.Any character except newlinea.c → "abc", "axc"
\\dDigit (0-9)\\d+ → "42", "007"
\\DNOT a digit
\\wWord char: [A-Za-z0-9_]
\\WNOT a word char
\\sWhitespace (space, tab, newline)
\\SNOT whitespace
[abc]Any of a, b, or c
[^abc]NOT a, b, or c
[a-z]Range: any lowercase letter
[a-zA-Z0-9]Combined ranges

Anchors & boundaries

^Start of string (or line, with /m flag)
$End of string (or line, with /m flag)
\\bWord boundary\\bcat\\b → matches "cat" not "category"
\\BNOT a word boundary
\\AAbsolute start of string (PCRE/Python)
\\ZEnd of string, before final newline (PCRE/Python)
\\zAbsolute end of string (PCRE)

Quantifiers

*Zero or more (greedy)
+One or more (greedy)
?Zero or one (optional)
{n}Exactly n times\\d{4} → exactly 4 digits
{n,}n or more times
{n,m}Between n and m times
*?Lazy (as few as possible)
+?Lazy one-or-more
??Lazy optional

Groups & capturing

(...)Capturing group(\\d+) captures digits
(?:...)Non-capturing group (groups without capturing)
(?<name>...)Named capture group
\\1, \\2…Backreference to numbered group
\\k<name>Backreference to named group
|Alternation (or)cat|dog → "cat" or "dog"

Lookarounds

(?=...)Positive lookahead — assert what followsa(?=b) → "a" only if followed by "b"
(?!...)Negative lookahead — assert what does NOT follow
(?<=...)Positive lookbehind — assert what precedes
(?<!...)Negative lookbehind

Special chars (escape these)

\\.Literal dot
\\\\Literal backslash
\\( \\)Literal parentheses
\\[ \\]Literal brackets
\\{ \\}Literal braces
\\? \\* \\+Literal quantifier chars
\\|Literal pipe
\\^ \\$Literal anchors

Escape sequences

\\nNewline (LF)
\\rCarriage return (CR)
\\tTab
\\0Null char
\\xHHHex char\\x41 → "A"
\\uHHHHUnicode char\\u00E9 → "é"
\\p{Letter}Unicode property (with /u flag)

Flags

iCase-insensitive/cat/i → "Cat", "CAT"
gGlobal — find all matches, not just the first
mMultiline — ^ and $ match per line
sDotAll — . matches newlines too
uUnicode-aware
ySticky — match from lastIndex (JS)

Common patterns

\\d{3}-\\d{4}US phone tail
[a-z]+@[a-z]+\\.[a-z]+Email (rough)
\\d{4}-\\d{2}-\\d{2}ISO date
https?://\\S+HTTP/HTTPS URL
#[0-9a-fA-F]{6}Hex color
^\\s*$Empty / whitespace line
(\\w+)\\s+\\1Repeated word

For rigorously-tested production-ready patterns, see the patterns library.

Tips that save you regex pain

  1. Always anchor when you mean to validate. A pattern without ^ and $ matches if it's found anywhere in the string — not what you usually want for validation.
  2. Use raw strings. In Python: r"\\d+". In JavaScript: prefer regex literals /\\d+/ over new RegExp("\\\\d+"), since string escaping doubles the backslashes.
  3. Be careful with greedy quantifiers. .* will match as much as possible, then backtrack. Use .*? (lazy) or [^"]* (specific) when extracting fields.
  4. Watch for catastrophic backtracking. Nested quantifiers like (a+)+ can cause exponential runtime on certain inputs. This is called ReDoS — see our explainer which warns about it.
  5. Comment complex regexes. Most flavors support an extended/verbose mode (x flag) that lets you spread regex across lines with comments.