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).
AnchorsCharacter classesQuantifiersGroups & alternationLookaroundsFlags (modifiers)Escaping special characters
Anchors
Match positions, not characters.
Token
Meaning
Example
^Start of string (or line with /m flag)
^Hello → "Hello world"
$End of string (or line with /m flag)
world$ → "Hello world"
\bWord boundary
\bcat\b → "the cat sat"
\BNot a word boundary
\Bcat → "scatter"
Character classes
Shorthand for sets of characters.
Token
Meaning
Example
.Any character except newline
h.llo → "hello", "h@llo"
\dAny digit (0-9)
\d{3} → "123"
\DAny non-digit
\D+ → "abc"
\wWord character (a-z, A-Z, 0-9, _)
\w+ → "user_42"
\WNon-word character
\W → "@", "!"
\sWhitespace (space, tab, \n)
\s+ → " "
\SNon-whitespace
\S+ → "hello"
[abc]Any of a, b, or c
[aeiou] → vowels
[^abc]Anything except a, b, c
[^0-9] → non-digits
[a-z]Range a to z
[A-Za-z] → letters
Quantifiers
Specify how many times the previous token must match.
Token
Meaning
Example
*0 or more
ab*c → "ac", "abbbc"
+1 or more
ab+c → "abc", "abbbc"
?0 or 1 (optional)
colou?r → "color", "colour"
{n}Exactly n
\d{4} → "2026"
{n,}n or more
\d{2,} → "42", "1234"
{n,m}Between n and m
\d{2,4} → "42", "1234"
*?, +?, ??Lazy (non-greedy)
a.*?b → minimal match
Groups & alternation
Capture, group, and choose between alternatives.
Token
Meaning
Example
(abc)Capturing group
(\d{3})-(\d{4})
(?:abc)Non-capturing group
(?:https?)://...
(?<name>abc)Named capturing group
(?<year>\d{4})
a|ba or b
cat|dog → "cat" or "dog"
\1Backreference to group 1
(\w+)\s\1 → "hi hi"
Lookarounds
Zero-width assertions — match position only.
Token
Meaning
Example
(?=...)Positive lookahead
foo(?=bar) → "foo" before "bar"
(?!...)Negative lookahead
foo(?!bar) → "foo" not before "bar"
(?<=...)Positive lookbehind
(?<=\$)\d+ → digits after $
(?<!...)Negative lookbehind
(?<!\$)\d+ → digits not after $
Flags (modifiers)
Change how the engine evaluates the pattern.
Token
Meaning
Example
gGlobal — find all matches
/cat/g
iCase-insensitive
/hello/i → "HELLO"
mMultiline — ^ and $ match line breaks
/^foo/m
sDotall — . matches newline
/foo.bar/s
uUnicode mode
/\p{L}+/u
ySticky — match from lastIndex
/foo/y
Escaping special characters
These need a backslash to match literally.
Token
Meaning
Example
\.Literal dot
\.com → ".com"
\\Literal backslash
\\n → "\n"
\*Literal asterisk
\*\* → "**"
\?, \+, \(, \)Other escaped meta-chars
\(test\)
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?