Regex Cheat Sheet
Searchable regular expression cheat sheet: character classes, anchors, quantifiers, groups, lookarounds, escapes, flags and replacement patterns with examples.
Character classes
. | Any character except a line break (any character with the s flag) a.c → abc, a-c |
\d | A digit 0–9 (with the u flag still only ASCII digits) \d+ → 2024 |
\D | Any character that is not a digit |
\w | A word character: A–Z, a–z, 0–9 or _ \w+ → user_42 |
\W | Any character that is not a word character |
\s | Whitespace: space, tab, line breaks and Unicode spaces |
\S | Any character that is not whitespace |
[abc] | One of the listed characters [aeiou] → e |
[^abc] | Any character except the listed ones |
[a-z] | A character in the range (here a to z) [0-9a-f]+ → ff00 |
\p{L} | Any letter in any script (needs the u flag) \p{L}+ (u) → Việt |
\p{Script=Han} | A character of a given script, e.g. Han, Latin, Cyrillic (needs the u flag) |
Anchors
^ | Start of the text (start of each line with the m flag) |
$ | End of the text (end of each line with the m flag) |
\b | Word boundary \bcat\b → cat (not concat) |
\B | Not a word boundary |
Quantifiers
* | 0 or more times (greedy) |
+ | 1 or more times (greedy) |
? | 0 or 1 time (optional) colou?r → color, colour |
{n} | Exactly n times \d{4} → 2024 |
{n,} | n or more times |
{n,m} | Between n and m times |
*? +? ?? | Lazy versions: match as few characters as possible <.+?> → <b> |
Groups and references
(abc) | Capturing group, available as $1, $2… |
(?:abc) | Group without capturing |
(?<name>abc) | Named capturing group (?<y>\d{4}) → y = 2024 |
\1 | Repeat the text matched by group 1 (\w)\1 → ll in hello |
\k<name> | Repeat the text matched by a named group |
a|b | Either the left or the right alternative cat|dog → dog |
Lookaround
(?=abc) | Followed by abc (not included in the match) \d+(?=px) → 12 in 12px |
(?!abc) | Not followed by abc |
(?<=abc) | Preceded by abc (not included in the match) (?<=\$)\d+ → 5 in $5 |
(?<!abc) | Not preceded by abc |
Escapes
\. \* \\ | Escape a special character to match it literally |
\n \t \r | Line feed, tab, carriage return |
\uFFFF | Character by 4-digit hex code |
\u{1F600} | Character by code point (needs the u flag) |
Flags
g | Find all matches, not just the first |
i | Case-insensitive matching |
m | ^ and $ match at line starts and ends |
s | The dot also matches line breaks |
u | Full Unicode mode: \p{…}, \u{…}, emoji as one character |
y | Match only at lastIndex (sticky) |
d | Report start and end indices of each group |
Replacement patterns
$1 | Text of capturing group 1 |
$<name> | Text of a named group |
$& | The whole match |
$` $' | Text before / after the match |
$$ | A literal $ sign |
Related Tools
View AllWhat is Regex Cheat Sheet?
A compact, searchable reference of JavaScript (ECMAScript) regular expression syntax. It covers character classes, Unicode property escapes, anchors, quantifiers (greedy and lazy), groups and backreferences, lookahead and lookbehind, escapes, all seven flags and the special patterns usable in replacement strings, with short examples.
How to use Regex Cheat Sheet
- Scroll through the sections, or type in the search box to filter by token (e.g. \b) or by words in the description (e.g. "lookbehind").
- Read the description and the example under each token.
- Try the pattern in the Regex Tester.
Example
Search: \d
\d – A digit 0–9 (with the u flag still only ASCII digits); \D – Any character that is not a digit
When to use Regex Cheat Sheet
Use it while writing regexes for JavaScript, TypeScript, Node.js and most modern regex engines. Engine-specific syntax (possessive quantifiers, atomic groups, \A, \z, inline modifiers in PCRE/Python) is not listed because JavaScript does not support it.
How Regex Cheat Sheet works
The entries are grouped by topic. The search box filters entries whose token or translated description contains your text, case-insensitively. Descriptions reflect ECMAScript behaviour, for example that \d matches only ASCII digits even with the u flag, that \p{…} needs the u flag, and that . does not match line breaks unless the s flag is set.
Common Use Cases
- Looking up the syntax for a named group or lookbehind
- Remembering what each regex flag does
- Checking replacement patterns like $& and $<name>
- Teaching regex basics with examples
FAQs
Does \w match accented letters?
No. In JavaScript \w is only A–Z, a–z, 0–9 and _. Use \p{L} with the u flag to match letters in any language.
What is the difference between greedy and lazy quantifiers?
Greedy quantifiers (*, +, {n,}) match as much as possible; adding ? (*?, +?) makes them match as little as possible, e.g. <.+?> matches one tag instead of everything up to the last >.
Does JavaScript support lookbehind?
Yes, (?<=…) and (?<!…) are supported in all modern browsers and Node.js.