devtools

Regex Cheatsheet

Every regex token, one search away.

client-sidefreeno-signup

Anchors

^
start of string
Matches the position before the first char.
^hello
$
end of string
Matches the position after the last char.
world$
\b
word boundary
Between a word char and a non-word char.
\bcat\b
\B
not a word boundary
Where \b does not match.
\Bcat

Character classes

.
any char
Any char except newline (with /s flag: also newline).
a.c
\d
digit
A digit [0-9].
\d+
\D
not a digit
[^0-9].
\D+
\w
word char
[A-Za-z0-9_].
\w+
\W
not a word char
[^A-Za-z0-9_].
\W+
\s
whitespace
Space, tab, newline.
\s+
\S
not whitespace
Any non-whitespace char.
\S+
[abc]
character set
Any of a, b, c.
[aeiou]
[^abc]
negated set
Any char except a, b, c.
[^0-9]
[a-z]
range
Chars between a and z (inclusive).
[A-Za-z]

Quantifiers

*
0 or more
Greedy; matches as much as possible.
a*
+
1 or more
Greedy; matches as much as possible.
a+
?
0 or 1
Optional; makes the preceding token optional.
colou?r
{n}
exactly n
Exactly n occurrences.
\d{4}
{n,}
n or more
At least n occurrences.
\d{2,}
{n,m}
between n and m
Between n and m (inclusive).
\d{2,4}
*?
lazy 0+
Matches as few as possible.
a*?
+?
lazy 1+
Matches as few as possible.
<.+?>

Groups & lookarounds

(abc)
capture group
Captures "abc" for backreferences.
(\d+)
(?:abc)
non-capture group
Groups without capturing.
(?:ab)+
(?<name>x)
named group
JS/Python 3.7+ named capture.
(?<year>\d{4})
\1
backreference
Matches what group 1 captured.
(\w)\1
(?=x)
lookahead
Asserts x follows (no consume).
\d(?=px)
(?!x)
neg. lookahead
Asserts x does not follow.
\d(?!px)
(?<=x)
lookbehind
Asserts x precedes (ES2018+).
(?<=%)\d+
(?<!x)
neg. lookbehind
Asserts x does not precede.
(?<!\$)\d+

Escapes

\.
literal dot
Escaped, matches a real dot.
example\.com
\\
literal backslash
Matches one backslash.
C:\\
\(
literal paren
Escaped, matches a real (.
\(x\)
\n
newline
Line feed (LF).
\n
\t
tab
Horizontal tab.
\t
\r
carriage return
CR (old Mac line ending).
\r

Common patterns

email
email (RFC-ish)
Pragmatic email matcher.
[\w.+-]+@[\w-]+\.[a-z]{2,}
url
http(s) url
Matches http/https URLs.
https?://[\w.-]+(?:/[\w./?=-]*)?
ipv4
IPv4 address
Four octets 0-255.
(?:\d{1,3}\.){3}\d{1,3}
hex color
#rgb / #rrggbb
CSS hex color.
#(?:[0-9a-f]{3}){1,2}
date
YYYY-MM-DD
ISO date.
\d{4}-\d{2}-\d{2}
phone
US phone
(123) 456-7890 or 123-456-7890.
\(?\d{3}\)?[-.\s]?\d{3}[-.]\d{4}
static reference · no data ever leaves your browser
// how to use

How to use the Regex Cheatsheet

Regular expressions have a notoriously dense syntax that is hard to keep in your head. This is a searchable quick-reference of the tokens that work across JavaScript, Python, Go and PCRE — anchors, character classes, quantifiers, groups, lookarounds and a handful of ready-made patterns for common jobs.

  1. 01Start typing in the search box — the table filters live across tokens, names and descriptions.
  2. 02Browse the sections (Anchors, Character classes, Quantifiers, Groups, Escapes, Common patterns) to jog your memory.
  3. 03Click the copy button on the right of any example to grab a working pattern, then paste it into the Regex Tester to try it on real text.

tips

  • Most regex bugs come from greedy quantifiers eating too much. Add a ? after * or + to make them lazy: <.+?> matches a single tag instead of the whole document.
  • JavaScript supports named groups ((?<year>\d4)) and lookbehind ((?<=…)) since ES2018. Older browsers (IE, old Safari) reject them — compile to a fallback if you need to support them.
  • For emails, URLs and HTML, prefer a permissive regex and validate server-side. A regex that accepts every valid email rejects almost nothing useful; a strict one blocks real addresses. [\w.+-]+@[\w-]+\.[a-z]{2,} is the sweet spot for client-side hints.

frequently asked

Which regex flavor does this cover?+

The reference targets the syntax shared by JavaScript, Python, Go, Java, Rust and PCRE — anchors, character classes, quantifiers, groups and lookarounds. Engine-specific extensions like possessive quantifiers or atomic groups are noted where they differ.

Why does my regex work on regex101 but not in JS?+

JavaScript supports lookaheads but historically not lookbehinds (until ES2018). Python uses (?P<name>) for named groups, JS uses (?<name>). The cheatsheet calls out these dialect differences inline.

Can I test a pattern against text here?+

The cheatsheet is a reference, not a tester. For live matching, hop over to the Regex Tester tool — paste a pattern and sample text to see captures and matches highlighted.