Cheat sheet

JavaScript regex cheat sheet

The whole syntax on one page, as of ECMAScript 2025. Tap any example to open it, with sample text, in the tester.

Characters and escapes

Most characters match themselves. These have special meanings or stand for characters that are hard to type.

SyntaxMeaningTry it
.Any character except line breaks (with the s flag: any character at all)./c.t/g
\d \DA digit 0–9 / anything that is not a digit./\d+/g
\w \WA word character A–Z, a–z, 0–9 or _ / anything else./\w+/g
\s \SWhitespace (spaces, tabs, line breaks, Unicode spaces) / anything else./\s+/g
\t \n \rTab, line feed and carriage return./\r?\n/g
\xhh \uhhhhA character by its hex code: \x41 is A, \u00e9 is é./caf\u00e9/
\u{hhhhh}Any code point, including emoji. Needs the u or v flag./\u{1F600}/u
\. \* \\A backslash makes a special character literal: \. is a dot, \\ a backslash./\d+\.\d+/g

Character classes

Square brackets match one character from a set.

SyntaxMeaningTry it
[abc]One of a, b or c./gr[ae]y/g
[^abc]Any one character except a, b and c./[^aeiou\s]+/g
[a-z0-9]A range: any character from a to z, or 0 to 9./[A-F0-9]{2}/gi
[\s\S]Any character including line breaks (the classic idiom before the s flag)./<b>[\s\S]*?</b>/g
\p{L} \P{L}A character with (or without) a Unicode property, here “letter”. Needs u or v./\p{L}+/gu
\p{Script=Greek}A character from one script (also Latin, Cyrillic, Han, Arabic …)./\p{Script=Greek}+/gu

Anchors and boundaries

These match a position, not a character.

SyntaxMeaningTry it
^Start of the text (with m: start of each line)./^\w+/gm
$End of the text (with m: end of each line)./\d+$/gm
\bWord boundary: between a word character and a non-word character./\bcat\b/g
\BNot a word boundary./\Bcat\B/g

Quantifiers

A quantifier repeats the item just before it. They are greedy (take as much as possible) unless followed by ?.

SyntaxMeaningTry it
*Zero or more times./go*gle/g
+One or more times./go+gle/g
?Optional: zero or one time./colou?r/g
{3} {2,} {2,4}Exactly 3, at least 2, between 2 and 4 times./\d{2,4}/g
*? +? ?? {2,4}?Lazy versions: as few repetitions as possible./<.+?>/g

Groups, alternation and backreferences

Parentheses group items, capture what they matched and limit the scope of |.

SyntaxMeaningTry it
a|bEither a or b; alternatives are tried left to right./cat|dog/g
(…)Capturing group: numbered from 1 by its opening parenthesis./(\d{4})-(\d{2})/g
(?<name>…)Named group, available as match.groups.name./(?<user>\w+)@(?<host>[\w.]+)/g
(?:…)Non-capturing group: groups without saving./(?:ab)+/g
\1 \k<name>Backreference: the same text a group matched earlier./(['"]).*?\1/g
(?i:…) (?-i:…)Modifiers: turn i, m or s on or off for part of the pattern (ES2025)./(?i:hello) World/g

Lookahead and lookbehind

Check what comes before or after a position without including it in the match.

SyntaxMeaningTry it
(?=…)Positive lookahead: followed by …/\d+(?=px)/g
(?!…)Negative lookahead: not followed by …/\d+(?![\dp])/g
(?<=…)Positive lookbehind: preceded by …/(?<=\$)\d+(?:\.\d\d)?/g
(?<!…)Negative lookbehind: not preceded by …/(?<!\$)\b\d+\b/g

Flags

Written after the closing slash (/…/gi) or as the second argument of new RegExp.

SyntaxMeaningTry it
gGlobal: find all matches, not just the first./o/g
iIgnore case./regex/gi
mMultiline: ^ and $ work per line./^#.*$/gm
sdotAll: . also matches line breaks./start.*end/s
uUnicode: code points, \u{…}, \p{…} and strict syntax./^.$/u
vUnicode sets: u plus set operations and string properties./[\p{L}--[a-z]]+/gv
ySticky: match only at lastIndex, without searching ahead./\d/gy
dIndices: record start and end positions of every group (match.indices)./(\w+)@/d

Unicode sets (v flag)

The v flag adds set operations inside classes and properties that match whole emoji sequences.

SyntaxMeaningTry it
[A--B]Subtraction: in A but not in B./[\p{L}--\p{ASCII}]+/gv
[A&&B]Intersection: in both A and B./[\p{L}&&\p{ASCII}]+/gv
[[a-z][0-9]]Nested classes (union)./[[a-f][0-9]]+/gv
\q{abc|def}String literals inside a class./[\q{Mr|Mrs|Ms}]\./gv
\p{RGI_Emoji}A whole emoji, including flags and joined sequences./\p{RGI_Emoji}/gv

Replacement patterns

Special sequences in the replacement string of replace() and replaceAll().

SyntaxMeaningTry it
$1 $2 …The text of group 1, 2 …/(\w+) (\w+)/
$<name>The text of a named group./(?<d>\d\d)\/(?<m>\d\d)\/(?<y>\d{4})/g
$&The whole match./\d+/g
$` $'The text before / after the match./=/
$$A literal dollar sign./(\d+)/g

JavaScript methods

Where a regex is used in JavaScript, and what each method returns.

SyntaxMeaningTry it
re.test(str)true or false. With g or y it starts from (and moves) re.lastIndex.
re.exec(str)The next match with groups and index, or null.
str.match(re)Without g: like exec. With g: an array of all matched texts.
str.matchAll(re)An iterator over every match with its groups (re must have g).
str.replace(re, x)Replaces the first match, or all of them with g; x may be a function.
str.replaceAll(re, x)Replaces every match (re must have g).
str.split(re)Splits at each match; captured groups are included in the result.
str.search(re)Index of the first match, or −1.

Notes on JavaScript’s flavour

JavaScript regexes are close to Perl and PCRE but not identical. There are no possessive quantifiers or atomic groups, no \A, \Z or \z (use ^ and $ without the m flag), and $ never matches before a final newline unless m is on. On the other hand, lookbehind can contain any pattern, even variable-length ones.

\d, \w and \b only know ASCII, even with the u flag. For other scripts use Unicode property escapes such as \p{L} (letters) or \p{Nd} (decimal digits), which need u or v.

When you build a regex from a string, new RegExp("\\d+"), every backslash must be doubled, because the string literal consumes one level of escaping. The regex literal /\d+/ does not have that problem.

Questions

What is the difference between u and v?

v (ES2024) includes everything u does and adds set operations in classes, nested classes, \q{…} strings and properties of strings such as \p{RGI_Emoji}. A few characters must be escaped inside classes in v mode, and you cannot use both flags at once.

Which browsers support lookbehind and named groups?

All current browsers. Safari was the last to add lookbehind, in version 16.4 (2023).

Why does test() give different answers on repeated calls?

With the g or y flag, test() and exec() start from lastIndex and move it after each match. Reset re.lastIndex = 0, or drop the g flag when you only need a yes or no.