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:

JavaScript Regex Cheat Sheet

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() and exec() calls return null
Language: JavaScript Filename: Literal or Constructor
/\d+/.test("abc 123");    // true
new RegExp("\\d+", "g");  // /\d+/g
new RegExp(/ab/g, "i");   // /ab/i
Language: JavaScript Filename: 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
Language: JavaScript Filename: Greedy vs. Lazy
"<a><b>".match(/<.*>/)[0];   // '<a><b>'
"<a><b>".match(/<.*?>/)[0];  // '<a>'

Flags

  • Prefer v over u in 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
Language: JavaScript Filename: Match Whole Code Points
"😀".length;              // 2
/^.$/.test("😀");         // false
/^.$/u.test("😀");        // true
/\p{L}+/u.exec("héllo")[0]; // 'héllo'
Language: JavaScript Filename: 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
  • \1 or \k<name> back-references a group
Language: JavaScript Filename: 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' }
Language: JavaScript Filename: Back-Reference a Match
"the the".match(/(\w+) \1/)[0];
// 'the the'
"the the".match(/(?<w>\w+) \k<w>/)[0];
// 'the the'
Language: JavaScript Filename: Unmatched Groups Are undefined
/(a)|(b)/.exec("b").slice(1);
// [ undefined, 'b' ]

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
Language: JavaScript Filename: 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 details
  • match() with g returns plain strings, no groups
  • matchAll() needs g and returns an iterator
Language: JavaScript Filename: 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
Language: JavaScript Filename: Every Match With Groups
const s = "a1b22";
[...s.matchAll(/(?<n>\d+)/g)]
  .map((m) => [m.groups.n, m.index]);
// [ [ '1', 1 ], [ '22', 3 ] ]
Language: JavaScript Filename: 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 without g
Token Inserts
$1 $<name> Group text
$& Whole match
$` $' Text before, after
$$ A literal $
Language: JavaScript Filename: Reorder With Groups
"John Smith".replace(
  /(?<f>\w+) (?<l>\w+)/,
  "$<l>, $<f>",
);                        // 'Smith, John'
Language: JavaScript Filename: 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
Language: JavaScript Filename: Build a Pattern From Text
RegExp.escape("a.b");     // '\\x61\\.b'
const re = new RegExp(
  RegExp.escape("a.b"), "g",
);
re.test("axb");           // false

lastIndex Gotchas

  • g and y regexes remember lastIndex
  • test() and exec() resume from it
  • A failed match resets it to 0
  • Do not reuse a stored g regex with test()
Language: JavaScript Filename: Alternating Results
const g = /a/g;
g.test("aa");   // true,  lastIndex 1
g.test("aa");   // true,  lastIndex 2
g.test("aa");   // false, lastIndex 0
Language: JavaScript Filename: Sticky Matches at One Spot
const y = /a/y;
y.lastIndex = 1;
y.test("ba");             // true
y.lastIndex;              // 2
/a/y.test("ba");          // false
Language: JavaScript Filename: 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

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:

JavaScript Regex Cheat Sheet