Regular expressions cheat sheet (JavaScript flavor)
The regex syntax you actually use, from character classes and quantifiers to groups, lookarounds and flags, with the JavaScript methods that apply them.
Not checked by a trusted editor yet Last edited Outdated?
A regular expression is a small pattern language for finding and replacing text. In JavaScript you write one between slashes, like /\d+/g, or build one from a string with new RegExp("\\d+", "g"). Most of this syntax also works in Python, Java, grep and editors, with small differences.
Characters and classes
.any character except a line break (with thesflag, including line breaks)\ddigit,\wletter, digit or underscore (ASCII only),\swhitespace. Uppercase\D \W \Smean the opposite.[abc]one of a, b, c.[a-z0-9]ranges.[^abc]anything except these.\p{L}any letter in any language,\p{Lu}uppercase letter,\p{N}number. Requires theuorvflag.\.\*\(escape special characters to match them literally.
Anchors and boundaries
^start and$end of the string (of each line with themflag).\bword boundary:/\bcat\b/matches cat but not concatenate.
Quantifiers
*zero or more,+one or more,?optional.{3}exactly 3,{2,5}2 to 5,{2,}2 or more.- Add
?to make any quantifier lazy (as few as possible):.*?.
"<b>x</b><b>y</b>".match(/<b>.*<\/b>/)[0]; // "<b>x</b><b>y</b>" greedy
"<b>x</b><b>y</b>".match(/<b>.*?<\/b>/)[0]; // "<b>x</b>" lazy
Groups and alternation
(abc)capturing group, referenced as$1in replacements and\1inside the pattern.(?<year>\d{4})named group, available asmatch.groups.yearand$<year>.(?:abc)group without capturing, for applying a quantifier or alternation.cat|dogeither side.
const m = "2026-10-15".match(/^(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})$/);
m.groups.year; // "2026"
"2026-10-15".replace(/(\d{4})-(\d{2})-(\d{2})/, "$3.$2.$1"); // "15.10.2026"
Lookarounds
Lookarounds check what is next to the match without including it:
(?=...)followed by,(?!...)not followed by.(?<=...)preceded by,(?<!...)not preceded by.
"price: $42, $7".match(/(?<=\$)\d+/g); // ["42", "7"]
Flags
gglobal: find all matches, not just the first.iignore case.mmultiline anchors.sdot matches newlines.uUnicode mode: correct handling of emoji and\p{...}.vis its newer superset with set operations like[\p{L}--[a-z]].ysticky: match only atlastIndex.dadds start and end indices for groups.
JavaScript methods
re.test(str): true or false.str.match(re): first match with groups, or an array of all matched strings withg.str.matchAll(re): an iterator of full match objects; the regex must haveg.str.replace(re, replacement)andreplaceAll: the replacement can be a string with$1, or a function.str.split(re): split on a pattern, for example/[,;\s]+/.
const log = "GET /a 200\nPOST /b 500";
for (const [, method, path, status] of log.matchAll(/^(\w+) (\S+) (\d{3})$/gm)) {
console.log(method, path, status);
}
Traps
- A regex with
goryrememberslastIndex. Callingtestrepeatedly on the same object alternates between true and false. Do not reuse global regexes fortest. - Building a pattern from user input requires escaping. Newer runtimes (Node 24+, current browsers) have
RegExp.escape(); otherwise uses.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"). - Nested quantifiers such as
(a+)+can take exponential time on unlucky input (catastrophic backtracking). Keep patterns specific, and limit input length for patterns that run on untrusted text. - Regexes are poor at nested structures. Use a real parser for HTML, JSON or programming languages.
See also
- 0016 let, const and var: variables and scope in JavaScript Use const by default, let when a value must change, and avoid var. The difference is about scope: where a variable is… Beginner · Concept · Any system Article Basics 15 Oct 2026
- 0017 JavaScript array methods cheat sheet: map, filter, reduce and friends A quick reference for the array methods you use every day, with what each one returns and whether it changes the original… Beginner · Reference · Any system Article Basics 15 Oct 2026
- 0026 Fix: TypeError: Cannot read properties of undefined (reading ‘x’) You tried to read a property from a value that is undefined or null. Find which value is missing, then guard it… Beginner · Fix · Any system Article Basics · Common errors 15 Oct 2026
History
Every saved edit is kept. The plot on the sleeve draws one line per edit. Open any edit to see exactly what changed.
- Commitpedia +3,697 chars