Regex Cheat Sheet

A compact reference for regular expression syntax that works across most flavors (JavaScript, PCRE, Python). Test patterns live with the Regex Tester.

Try it live: Regex TesterOpen Regex Tester

Anchors

^Start of string (or line with m flag)
$End of string (or line with m flag)
\bWord boundary
\BNot a word boundary

Character classes

.Any character except newline
\d \DDigit / non-digit
\w \WWord char [A-Za-z0-9_] / non-word
\s \SWhitespace / non-whitespace
[abc]Any of a, b, or c
[^abc]Not a, b, or c
[a-z]Range a to z

Quantifiers

*0 or more
+1 or more
?0 or 1 (optional)
{n}Exactly n
{n,}n or more
{n,m}Between n and m
*? +?Lazy (non-greedy) versions

Groups & lookarounds

(...)Capturing group
(?:...)Non-capturing group
(?<name>...)Named capturing group
a|bAlternation (a or b)
(?=...)Positive lookahead
(?!...)Negative lookahead
(?<=...)Positive lookbehind

Flags

gGlobal — all matches
iCase-insensitive
mMultiline (^ and $ per line)
sDotall (. matches newline)

Common patterns

^\d+$Integer only
[\w.+-]+@[\w-]+\.[\w.-]+Basic email
https?://[^\s]+URL
^#?[0-9a-fA-F]{6}$Hex color

More cheat sheets