Skip to content
Commitpedia
Log in Join & edit

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.

Edit history of Regular expressions cheat sheet (JavaScript flavor): 1 recorded edit
Edit history · 1 edit
Type
Article
Edits
1
Editors
1
First issued
15 Oct 2026
Last edit
15 Oct 2026
Reading
3 min

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 the s flag, including line breaks)
  • \d digit, \w letter, digit or underscore (ASCII only), \s whitespace. Uppercase \D \W \S mean 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 the u or v flag.
  • \. \* \( escape special characters to match them literally.

Anchors and boundaries

  • ^ start and $ end of the string (of each line with the m flag).
  • \b word 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 $1 in replacements and \1 inside the pattern.
  • (?<year>\d{4}) named group, available as match.groups.year and $<year>.
  • (?:abc) group without capturing, for applying a quantifier or alternation.
  • cat|dog either 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

  • g global: find all matches, not just the first.
  • i ignore case. m multiline anchors. s dot matches newlines.
  • u Unicode mode: correct handling of emoji and \p{...}. v is its newer superset with set operations like [\p{L}--[a-z]].
  • y sticky: match only at lastIndex. d adds 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 with g.
  • str.matchAll(re): an iterator of full match objects; the regex must have g.
  • str.replace(re, replacement) and replaceAll: 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 g or y remembers lastIndex. Calling test repeatedly on the same object alternates between true and false. Do not reuse global regexes for test.
  • Building a pattern from user input requires escaping. Newer runtimes (Node 24+, current browsers) have RegExp.escape(); otherwise use s.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.

  1. commitpedia-admin +3,697 chars

Report a problem

Something wrong, unsafe or copied? Tell the editors.
What is wrong?