RegexBuilder
← Back to the tools
📋

Regex Cheat Sheet

One page, every regex building block you will reach for, with the JavaScript, Python and PCRE notes that matter. Copy a pattern into the RegexBuilder tester to see it work.

Metacharacters

.Any single character (except line terminators by default)
\Escape the next character (make a metacharacter literal)
|Alternation — match either side
( )Group and capture
[ ]Character class — any one character from the set
^ $Start / end anchors (or line, with the m flag)

Character classes

[abc]a, b or c
[a-z]Any lowercase letter from a to z
[^abc]Any character except a, b or c
\dA digit (0-9)
\wA word character: letter, digit or underscore
\sWhitespace: space, tab, newline
\D \W \SThe opposites of \d \w \s

Quantifiers

*Zero or more (greedy)
+One or more
?Zero or one (optional)
{n}Exactly n
{n,}n or more
{n,m}Between n and m
*? +? ?? {n,m}?The lazy versions — match as little as possible

Anchors & boundaries

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

Groups & backreferences

(abc)Capture group #1
(?:abc)Non-capturing group
(?<name>abc)Named group "name"
\1Backreference to group 1
\k<name>Backreference to a named group

Lookarounds

(?=abc)Positive lookahead — abc must follow
(?!abc)Negative lookahead — abc must NOT follow
(?<=abc)Positive lookbehind — abc must precede
(?<!abc)Negative lookbehind — abc must NOT precede

Flags

gGlobal — find every match (JS/PCRE; Python: re.findall)
iCase-insensitive
mMultiline — ^ and $ match each line
sDot-all — . matches newlines too
uUnicode mode (JS)

JavaScript, Python & PCRE differences

Named groups

JS: (?<name>) and \k<name>. Python: also (?<name>) since 3.8 (legacy (?P<name>)). PCRE: (?<name>).

Lookbehind

JS: variable-length lookbehind supported. Python: fixed-length only. PCRE2: variable-length supported.

\d semantics

Python re: matches Unicode digits. JS and PCRE: ASCII digits by default.

Global matching

JS and PCRE use the /g flag; Python has no /g — call re.findall or re.finditer.

Comments

PCRE allows x mode with comments; JS uses inline (?#…) only with the x flag in newer engines.

Try the interactive tester