JavaScript Regex Cheat Sheet
This regex cheat sheet condenses JavaScript regular expressions into one scannable reference, from flags and groups to lookarounds and lastIndex. You can also download the information as a printable cheat sheet:
Free Bonus: JavaScript Regex Cheat Sheet
Get the JavaScript Regex Cheat Sheet (PDF), a printable reference to regex literals, flags, character classes, named groups, lookarounds, match and replace methods, RegExp.escape, and lastIndex gotchas:
Want a guided path instead of a reference? Start learning at realjs.com.
Getting Started
- Prefer a literal
/\d+/, since it needs no string escaping - Use
new RegExp(str, flags)only for dynamic patterns - Failed
match()andexec()calls returnnull
Literal or Constructor
/\d+/.test("abc 123"); // true
new RegExp("\\d+", "g"); // /\d+/g
new RegExp(/ab/g, "i"); // /ab/i
Find Your First Match
"abc 123 xyz".match(/\d+/);
// [ '123', index: 4,
// input: 'abc 123 xyz',
// groups: undefined ]
Characters and Quantifiers
- Quantifiers are greedy until you add
? - Escape
. * + ? ^ $ { } ( ) | [ ] \
| Pattern | Matches |
|---|---|
. |
Any char except line break |
\d \w \s |
Digit, word, space |
\D \W \S |
The opposites |
[aeiou] |
Any one listed char |
[^aeiou] |
Any char not listed |
\p{L} |
Letter (needs u or v) |
^ $ |
Start, end (line with m) |
\b |
Word boundary |
| Pattern | Repeats |
|---|---|
* + ? |
0+, 1+, 0 or 1 |
{m} {m,n} |
Exactly m, m to n |
*? +? |
Lazy versions |
Greedy vs. Lazy
"<a><b>".match(/<.*>/)[0]; // '<a><b>'
"<a><b>".match(/<.*?>/)[0]; // '<a>'
Flags
- Prefer
voveruin new code, but never combine them - Read back flags with
re.flags
| Flag | Effect |
|---|---|
g |
Global: find every match |
i |
Ignore case |
m |
^ $ match each line |
s |
. also matches \n |
u |
Unicode code points |
v |
Unicode sets: [\p{L}--[a-z]] |
y |
Sticky: match at lastIndex |
d |
Add indices to matches |
Match Whole Code Points
"😀".length; // 2
/^.$/.test("😀"); // false
/^.$/u.test("😀"); // true
/\p{L}+/u.exec("héllo")[0]; // 'héllo'
Locate Groups With d
const m = /(\d+)-(\d+)/d.exec("ab 12-34");
m.indices[1]; // [ 3, 5 ]
Groups and Alternation
(...)captures,(?:...)only groups|binds loosest, so write(cat|dog)s\1or\k<name>back-references a group
Name Your Groups
const m = "2026-09".match(
/(?<y>\d{4})-(?<m>\d{2})/,
);
m.groups.y; // '2026'
m.groups;
// [Object: null prototype]
// { y: '2026', m: '09' }
Back-Reference a Match
"the the".match(/(\w+) \1/)[0];
// 'the the'
"the the".match(/(?<w>\w+) \k<w>/)[0];
// 'the the'
Unmatched Groups Are undefined
/(a)|(b)/.exec("b").slice(1);
// [ undefined, 'b' ]
Free Bonus: Download the JavaScript Regex Cheat Sheet PDF and keep the essentials at hand.
Lookarounds
- Lookarounds match a position and consume nothing
- Unlike Python, lookbehind may be variable width
| Pattern | Asserts |
|---|---|
(?=...) |
Followed by |
(?!...) |
Not followed by |
(?<=...) |
Preceded by |
(?<!...) |
Not preceded by |
Match Only in Context
"$20 30".match(/(?<=\$)\d+/g);
// [ '20' ]
"foobar fooqux".match(/foo(?!bar)/g);
// [ 'foo' ]
Matching Methods
test()returns a boolean,match()the detailsmatch()withgreturns plain strings, no groupsmatchAll()needsgand returns an iterator
Test, Exec, Match
/\d/.test("a1"); // true
/(\d)/.exec("a1")[1]; // '1'
"a1b22".match(/\d+/g); // [ '1', '22' ]
"abc".match(/x/g); // null
"abc".search(/c/); // 2
Every Match With Groups
const s = "a1b22";
[...s.matchAll(/(?<n>\d+)/g)]
.map((m) => [m.groups.n, m.index]);
// [ [ '1', 1 ], [ '22', 3 ] ]
Split on a Pattern
"a, b;c".split(/[,;]\s*/);
// [ 'a', 'b', 'c' ]
"a1b2".split(/(\d)/); // Keeps captures
// [ 'a', '1', 'b', '2', '' ]
Replacing
- A string pattern replaces only the first match
replaceAll()throws on a regex withoutg
| Token | Inserts |
|---|---|
$1 $<name> |
Group text |
$& |
Whole match |
$` $' |
Text before, after |
$$ |
A literal $ |
Reorder With Groups
"John Smith".replace(
/(?<f>\w+) (?<l>\w+)/,
"$<l>, $<f>",
); // 'Smith, John'
Compute the Replacement
"a1b22".replace(/\d+/g, (n) => n * 2);
// 'a2b44'
"a.b.c".replaceAll(".", "-");
// 'a-b-c'
"a-b".replace("-", () => "$&");
// 'a$&b'
Escaping Input
RegExp.escape()(ES2025) makes user input literal- It escapes a leading letter or digit, and spaces
Build a Pattern From Text
RegExp.escape("a.b"); // '\\x61\\.b'
const re = new RegExp(
RegExp.escape("a.b"), "g",
);
re.test("axb"); // false
Free Bonus: Download the JavaScript Regex Cheat Sheet PDF and keep the essentials at hand.
lastIndex Gotchas
gandyregexes rememberlastIndextest()andexec()resume from it- A failed match resets it to
0 - Do not reuse a stored
gregex withtest()
Alternating Results
const g = /a/g;
g.test("aa"); // true, lastIndex 1
g.test("aa"); // true, lastIndex 2
g.test("aa"); // false, lastIndex 0
Sticky Matches at One Spot
const y = /a/y;
y.lastIndex = 1;
y.test("ba"); // true
y.lastIndex; // 2
/a/y.test("ba"); // false
Loop With exec
const re = /\d/g;
let m;
while ((m = re.exec("a1b2"))) {
console.log(m[0], m.index);
} // 1 1, then 2 3
Want to go deeper on JavaScript regex?
RealJS teaches the mental models that let you write, read, and review modern JavaScript with confidence, whether you or an AI wrote it.
Learn more at realjs.com 💡
You can download this information as a printable cheat sheet:
Free Bonus: JavaScript Regex Cheat Sheet
Get the JavaScript Regex Cheat Sheet (PDF), a printable reference to regex literals, flags, character classes, named groups, lookarounds, match and replace methods, RegExp.escape, and lastIndex gotchas: