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.
| Token | Meaning | Example / matches |
|---|---|---|
. | Any character except newline (unless the `s` flag is set) | a.c
|
\d | Any digit — equivalent to [0-9] | \d+
|
\D | Any non-digit — equivalent to [^0-9] | \D+
|
\w | Word character — [A-Za-z0-9_] | \w+
|
\W | Non-word character — [^A-Za-z0-9_] | \W
|
\s | Whitespace — space, tab, newline, and more | \s+
|
\S | Non-whitespace | \S+
|
Anchors
Zero-width matches that assert position. They don't consume characters — they only match between characters.
| Token | Meaning | Example / matches |
|---|---|---|
^ | Start of string (or start of line with the `m` flag) | ^hello
|
$ | End of string (or end of line with the `m` flag) | world$
|
\b | Word boundary — position between \w and \W (or start/end) | \bcat\b
|
\B | Non-word boundary — the opposite of \b | \Bcat
|
Quantifiers
Repeat the preceding token. Add `?` after any quantifier to switch it from greedy (default) to lazy.
| Token | Meaning | Example / matches |
|---|---|---|
* | 0 or more times | ab*
|
+ | 1 or more times | ab+
|
? | 0 or 1 time (optional) | colou?r
|
{n} | Exactly n times | a{3}
|
{n,} | n or more times | a{2,}
|
{n,m} | Between n and m times | a{2,4}
|
*? +? ?? {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.
| Token | Meaning | Example / matches |
|---|---|---|
(...) | Capture group — remembered as $1, $2, … in replacement | (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
|
\1 … \9 | Backreference in the pattern — must match the same text captured by group N | (\w+) \1
|
Lookaround (zero-width assertions)
Peek at what comes before or after without consuming it. Great for 'match X, but only when Y is nearby'.
| Token | Meaning | Example / matches |
|---|---|---|
(?=...) | Positive lookahead — assert the next chars match | \d+(?= dollars)
|
(?!...) | Negative lookahead — assert the next chars do NOT match | foo(?!bar)
|
(?<=...) | Positive lookbehind — assert what precedes matches | (?<=\$)\d+
|
(?<!...) | Negative lookbehind — assert what precedes does NOT match | (?<!\$)\d+
|
Character classes
Match one character from a custom set. Inside `[...]`, most metacharacters lose their special meaning.
| Token | Meaning | Example / matches |
|---|---|---|
[abc] | Any one of a, b, or c | [aeiou]
|
[a-z] | Range — any lowercase Latin letter | [A-Za-z0-9_]
|
[^abc] | Negated class — any character that is NOT a, b, or c | [^0-9]
|
\p{L} | Unicode property — any Unicode letter (requires the `u` flag) Requires the `u` flag on the regex. | \p{L}+
|
\p{N} | Unicode property — any Unicode digit / numeric Requires the `u` flag. | \p{N}+
|
Flags
Modify how the regex engine runs. In JavaScript literals: `/pattern/gi`. In `new RegExp('pattern', 'gi')`.
| Token | Meaning | Example / 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
|
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.
| Token | Meaning | Example / 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
|
Replacement tokens
Special sequences you can use in a replacement string (String.prototype.replace, Python re.sub with named refs, etc.).
| Token | Meaning | Example / 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.
| Task | Pattern | |
|---|---|---|
| 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).