Regex Cheatsheet

Every regex token, on one page. Anchors, character classes, quantifiers, groups, lookarounds, flags. Works in JavaScript, Python, PHP, Java and Go (with minor flavor differences).

Anchors

Match positions, not characters.

Token
Meaning
^
Start of string (or line with /m flag)
$
End of string (or line with /m flag)
\b
Word boundary
\B
Not a word boundary

Character classes

Shorthand for sets of characters.

Token
Meaning
.
Any character except newline
\d
Any digit (0-9)
\D
Any non-digit
\w
Word character (a-z, A-Z, 0-9, _)
\W
Non-word character
\s
Whitespace (space, tab, \n)
\S
Non-whitespace
[abc]
Any of a, b, or c
[^abc]
Anything except a, b, c
[a-z]
Range a to z

Quantifiers

Specify how many times the previous token must match.

Token
Meaning
*
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)

Groups & alternation

Capture, group, and choose between alternatives.

Token
Meaning
(abc)
Capturing group
(?:abc)
Non-capturing group
(?<name>abc)
Named capturing group
a|b
a or b
\1
Backreference to group 1

Lookarounds

Zero-width assertions — match position only.

Token
Meaning
(?=...)
Positive lookahead
(?!...)
Negative lookahead
(?<=...)
Positive lookbehind
(?<!...)
Negative lookbehind

Flags (modifiers)

Change how the engine evaluates the pattern.

Token
Meaning
g
Global — find all matches
i
Case-insensitive
m
Multiline — ^ and $ match line breaks
s
Dotall — . matches newline
u
Unicode mode
y
Sticky — match from lastIndex

Escaping special characters

These need a backslash to match literally.

Token
Meaning
\.
Literal dot
\\
Literal backslash
\*
Literal asterisk
\?, \+, \(, \)
Other escaped meta-chars

Language flavors at a glance

Language
Notes
JavaScript
ECMAScript engine. Lookbehind supported (modern browsers). No POSIX classes.
Python
Use re module. Raw strings r"…" recommended. Supports named groups (?P<name>…).
PHP
PCRE — closest to Perl. Pattern needs delimiters: /pattern/flags or #pattern#flags.
Java
java.util.regex.Pattern. Backslashes need to be doubled in string literals.
Go
RE2 engine — no lookbehind, no backreferences. Otherwise PCRE-compatible.
Ruby
Onigmo engine. Built-in support for named groups (?<name>…).
C# / .NET
Full feature set including balanced groups. Verbatim strings @"…" help.

Ready to use a tested pattern instead of writing one from scratch?