DevKits
50 tokens · 9 sections

Regex Cheat Sheet

Every regex token you actually use, on one page. Written for JavaScript / ECMAScript flavor — Python, Go, Java, and PCRE share ~95% of these — with runnable examples for every entry.

Skim the section index below, jump to what you need, and hit the copy button on any pattern to paste it into the Regex Tester or Regex Replace to try it live. For ready-made patterns for common tasks (emails, URLs, IPv4, UUIDs, ISO dates), see the Regex Cookbook.

Metacharacters (single-character)

Shortcuts that match one character from a predefined set. Use their uppercase form to negate.

TokenMeaningExample / matches
.

Any character except newline (unless the `s` flag is set)

a.c
  • abc
  • a c
  • a9c
  • ac
  • a\nc
\d

Any digit — equivalent to [0-9]

\d+
  • 7
  • 42
  • 2026
  • seven
\D

Any non-digit — equivalent to [^0-9]

\D+
  • abc
  • hello!
\w

Word character — [A-Za-z0-9_]

\w+
  • hello
  • user_42
  • -
  • .
\W

Non-word character — [^A-Za-z0-9_]

\W
  • !
  • -
\s

Whitespace — space, tab, newline, and more

\s+
  • \t
  • \n
\S

Non-whitespace

\S+
  • hello
  • 42!

Anchors

Zero-width matches that assert position. They don't consume characters — they only match between characters.

TokenMeaningExample / matches
^

Start of string (or start of line with the `m` flag)

^hello
  • hello world
  • say hello
$

End of string (or end of line with the `m` flag)

world$
  • hello world
  • worldwide
\b

Word boundary — position between \w and \W (or start/end)

\bcat\b
  • a cat sat
  • catalog
\B

Non-word boundary — the opposite of \b

\Bcat
  • scatter
  • a cat

Quantifiers

Repeat the preceding token. Add `?` after any quantifier to switch it from greedy (default) to lazy.

TokenMeaningExample / matches
*

0 or more times

ab*
  • a
  • ab
  • abbbb
+

1 or more times

ab+
  • ab
  • abbbb
  • a
?

0 or 1 time (optional)

colou?r
  • color
  • colour
{n}

Exactly n times

a{3}
  • aaa
  • aa
  • aaaa
{n,}

n or more times

a{2,}
  • aa
  • aaaaaa
  • a
{n,m}

Between n and m times

a{2,4}
  • aa
  • aaa
  • aaaa
*? +? ?? {n,m}?

Lazy versions — match as few characters as possible

Prefer lazy quantifiers when parsing HTML-ish or delimited content — the #1 fix for 'regex matches too much'.

.*?</b>

Groups & Alternation

Groups let you apply a quantifier to more than one character, capture text for later use, or organize alternatives.

TokenMeaningExample / matches
(...)

Capture group — remembered as $1, $2, … in replacement

(cat|dog)
  • cat
  • dog
(?:...)

Non-capture group — same grouping, no memory (faster)

(?:https?://)?example.com
(?<name>...)

Named capture group — use $<name> in replacement, \k<name> in pattern

ECMAScript 2018+. Widely supported now.

(?<year>\d{4})-(?<month>\d{2})
|

Alternation — matches expression on the left OR right

yes|no|maybe
  • yes
  • no
  • maybe
\1 … \9

Backreference in the pattern — must match the same text captured by group N

(\w+) \1
  • hello hello
  • the the
  • hello world

Lookaround (zero-width assertions)

Peek at what comes before or after without consuming it. Great for 'match X, but only when Y is nearby'.

TokenMeaningExample / matches
(?=...)

Positive lookahead — assert the next chars match

\d+(?= dollars)
  • I paid 42 dollars → 42
(?!...)

Negative lookahead — assert the next chars do NOT match

foo(?!bar)
  • foobaz
  • foo
  • foobar
(?<=...)

Positive lookbehind — assert what precedes matches

(?<=\$)\d+
  • Price $42 → 42
(?<!...)

Negative lookbehind — assert what precedes does NOT match

(?<!\$)\d+
  • quantity 5
  • year 2026
  • $5

Character classes

Match one character from a custom set. Inside `[...]`, most metacharacters lose their special meaning.

TokenMeaningExample / matches
[abc]

Any one of a, b, or c

[aeiou]
  • a
  • e
  • u
[a-z]

Range — any lowercase Latin letter

[A-Za-z0-9_]
  • a
  • Z
  • 5
  • _
[^abc]

Negated class — any character that is NOT a, b, or c

[^0-9]
  • a
  • !
\p{L}

Unicode property — any Unicode letter (requires the `u` flag)

Requires the `u` flag on the regex.

\p{L}+
  • hello
  • café
  • 你好
\p{N}

Unicode property — any Unicode digit / numeric

Requires the `u` flag.

\p{N}+
  • 42
  • ٤٢
  • 四二

Flags

Modify how the regex engine runs. In JavaScript literals: `/pattern/gi`. In `new RegExp('pattern', 'gi')`.

TokenMeaningExample / matches
g

Global — find all matches, not just the first

Required for matchAll(), replaceAll(), and to iterate matches.

/foo/g
i

Case-insensitive

/hello/i
  • Hello
  • HELLO
m

Multiline — ^ and $ match line boundaries, not only string boundaries

/^foo/m
s

dotAll — `.` also matches newlines

Without `s`, `.` never matches \n.

/hello.world/s
u

Unicode — enables \p{...}, surrogate-pair-safe iteration, stricter escape rules

/\p{Emoji}/u
y

Sticky — match starts at regex.lastIndex, no scanning ahead

/foo/y

Escape sequences

Special characters that represent things you can't type directly. Also: use a backslash to make a metacharacter literal.

TokenMeaningExample / matches
\n \r \t

Newline, carriage return, tab

\t\d+
\0

Null character (U+0000)

\xhh

Character with hex value hh (2 digits)

\x41 = 'A'
\uhhhh

Character with hex value hhhh (4 digits)

\u2603 = ☃
\u{hhhhh}

Character by Unicode code point (any length, requires `u` flag)

\u{1F600} = 😀
\. \* \? \+ \( \[ \{ \| \\

Escape a metacharacter to match it literally

3\.14
  • 3.14
  • 3x14

Replacement tokens

Special sequences you can use in a replacement string (String.prototype.replace, Python re.sub with named refs, etc.).

TokenMeaningExample / matches
$&

The entire match

re.replace: "$&" → whole match
$1 $2 … $9

Text captured by the Nth group

/(\w+) (\w+)/ → "$2, $1"
$<name>

Text captured by a named group

/(?<y>\d{4})/ → "year=$<y>"
$`

The text before the match

$'

The text after the match

$$

A literal dollar sign

"price: $$5"

Common patterns

Copy-paste ready patterns for the tasks people search for most. Click any row for a deep-dive with language-specific snippets in the Cookbook.

TaskPattern
Email address^[^\s@]+@[^\s@]+\.[^\s@]+$
URL (http/https)https?://[^\s]+
IPv4 address^(?:\d{1,3}\.){3}\d{1,3}$
UUID v4^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$
Hex color^#(?:[0-9a-fA-F]{3}){1,2}$
ISO date (YYYY-MM-DD)^\d{4}-\d{2}-\d{2}$
Integer^-?\d+$
Positive float^\d+(?:\.\d+)?$
US phone number^\+?1?[-.\s]?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$
Extra whitespace\s{2,}
Empty line^\s*$
camelCase identifier^[a-z][a-zA-Z0-9]*$

Try a pattern live

All patterns on this page work in the browser. Paste one into a live tool to see what it matches (or how to search-and-replace with it).