Regex Quick Reference

Want hands-on practice? Try the interactive Regex Tester — live matches, capture groups, and replace preview.

The 20% of regular expressions you use 95% of the time — one page, copy-paste ready.

Character classes

.          any character except newline
\d         digit [0-9]            \D  non-digit
\w         word char [A-Za-z0-9_]  \W  non-word char
\s         whitespace             \S  non-whitespace
[abc]      any of a, b, c         [^abc]  none of a, b, c
[a-z]      range a–z              [0-9]   range 0–9
\p{L}      any letter (needs u flag)   \p{N}  any number

Quantifiers

*      0 or more        +      1 or more        ?   0 or 1
{n}    exactly n      {n,}   n or more      {n,m}  n to m
*? +? ??  lazy versions — match as few as possible

Anchors & boundaries

^   start of string (or line with m)     $   end of string (or line with m)
\b  word boundary      \B  not a word boundary

Groups & lookaround

(abc)      capture group            (?:abc)   non-capturing group
(a|b)      alternation              \1          backreference to group 1
(?=abc)    positive lookahead       (?!abc)    negative lookahead
(?<=abc)   positive lookbehind      (?<!abc)   negative lookbehind

Flags (JavaScript)

g  global — find all matches, not just the first
i  case-insensitive
m  multiline — ^/$ match line starts/ends
s  dotAll — . also matches newlines
u  unicode — enables \p{…}, correct emoji handling

Copy-paste patterns

date YYYY-MM-DD     ^\d{4}-\d{2}-\d{2}$
IPv4 address        ^(\d{1,3}\.){3}\d{1,3}$  (loose — add range checks for strict)
simple email        ^[^@\s]+@[^@\s]+\.[^@\s]+$  (pragmatic, not RFC-complete)
URL                 ^https?:\/\/[^\s/$.?#].[^\s]*$
Chinese mobile      ^1[3-9]\d{9}$
trim whitespace     ^\s+|\s+$  → replace with empty string
collapse blank lines ^\s*$\n with gm → replace with empty string

Frequently asked questions

Why is my .* eating too much?

Quantifiers are greedy by default. Use the lazy form .*?, or better, a negated class like [^"]* when you know the delimiter.

Is there a perfect email regex?

No — the RFC grammar is enormous. The pragmatic pattern above rejects obvious junk; for real validation, send a confirmation email.

Do these work in Python / grep / VS Code?

Mostly yes — the core syntax is shared. Watch for flavor differences: lookbehind and \p{…} support vary; VS Code search uses JavaScript regex.