Skip to content
QuicklySorted

JavaScript regex cheat sheet

A quick reference to JavaScript regular expressions: the characters, quantifiers, groups, lookarounds, and flags you will use most, with a working example of each and the traps that catch people. Every result shown here was produced by running the pattern in JavaScript.

Checked against the sources below on .

Characters

Characters and character classes
PatternMatches
.Any character except a line break, unless the s flag is on
\d \DA digit 0 to 9, and anything that is not one
\w \WA letter A to Z or a to z, a digit, or an underscore, and anything else
\s \SWhitespace, such as a space, tab, or line break, and anything else
[abc]One of a, b, or c
[^abc]Any one character except a, b, or c
[a-z0-9]One character in either range
\p{L}Any letter in any language, with the u flag
\. \\ \/A literal dot, backslash, or slash

\w covers only the English alphabet. To match words such as Ünïcødé, use \p{L}+ with the u flag: /\p{L}+/gu finds Ünïcødé in Ünïcødé 123.

Anchors and quantifiers

Anchors and quantifiers
PatternMeans
^ $The start and end of the text, or of each line with the m flag
\bA word boundary: /\bcat\b/g finds cat in cat catalog bobcat, but not the other two
* + ?Zero or more, one or more, zero or one
{3} {2,} {2,4}Exactly 3, at least 2, between 2 and 4
*? +? ??The same, but as few as possible

Quantifiers are greedy unless you add ?. On the text <b>one</b><b>two</b>, the pattern <b>.*</b> matches the whole string, while <b>.*?</b> matches only <b>one</b>.

Groups

Groups and references
PatternDoes
(abc)Captures a group, numbered from 1
(?<year>\d{4})Captures a named group, read as match.groups.year
(?:abc)Groups without capturing
a|bEither a or b
\1 \k<year>Matches the same text a group captured earlier
$1 $<year>Inserts a group in a replacement
Named groups, then a replacement that reorders them
const date = /(?<y>\d{4})-(?<m>\d{2})-(?<d>\d{2})/;

"2026-11-14".match(date).groups;
// { y: "2026", m: "11", d: "14" }

"2026-11-14".replace(date, "$<d>/$<m>/$<y>");
// "14/11/2026"

Lookahead and lookbehind

Lookahead and lookbehind
PatternMatchesExample result
a(?=b)a only when b follows/\d+(?=px)/g on 100px 2em 30px gives 100 and 30
a(?!b)a only when b does not follow/\bfile\.(?!js\b)\w+/g on file.js file.ts file.jsx gives file.ts and file.jsx
(?<=b)aa only when b comes before it/(?<=\$)\d+/g on $5 and €7 and $12 gives 5 and 12
(?<!b)aa only when b does not come before itThe opposite of the row above

Lookarounds check text without including it in the match. Lookbehind arrived in JavaScript with ES2018 and works in every current browser.

Flags

JavaScript regex flags
FlagNameEffect
gglobalFind every match, not just the first
iignore case/hello/i matches HeLLo
mmultiline^ and $ match at the start and end of each line
sdot all. also matches line breaks
uunicodeReads the pattern as Unicode, so an emoji counts as one character and \p{...} works
ystickyMatches only at exactly lastIndex
dindicesAdds the start and end of each group to the match
vunicode setsA newer form of u that allows set operations in classes, such as [\p{L}--[a-z]]

Without the u flag, JavaScript counts an emoji such as 😀 as two characters. With it, /./u matches the whole emoji.

Common patterns

Patterns people look up most
To matchPatternNotes
A whole number^\d+$Add -? at the start to allow negatives
A hex colour^#(?:[0-9a-f]{3}){1,2}$ with iAccepts #abc and #1a2B3c, rejects #abcd
A rough email check^[^\s@]+@[^\s@]+\.[^\s@]+$Catches typos only. Send a confirmation email to be sure
A date as YYYY-MM-DD^\d{4}-\d{2}-\d{2}$Checks the shape, not that the date exists
Commas with any spaces around them\s*,\s*"a, b,c ,d".split(/\s*,\s*/) gives a, b, c, d

Traps

  • lastIndex with the g flag. const re = /a/g; then re.test("aa") three times gives true, true, false, because each call starts where the last match ended.
  • Forgetting to escape. /a.c/ matches abc as well as a.c. Write /a\.c/ for a literal dot.
  • Catastrophic backtracking. Nested quantifiers such as (a+)+$ can take seconds or longer on text that almost matches. Make each part match only one way.
  • Building a pattern from user input. Escape the input first, or a value such as a+ or ( changes the pattern's meaning.
  • Using a regex to parse HTML or JSON. Use a parser; patterns break on nesting, quotes, and comments. For JSON, the JSON Formatter parses it and points to the first error.

Try any pattern on your own text in the Regex Tester. It uses your browser's JavaScript engine, supports the g, i, m, s, and u flags, lists every match with its position and groups, previews a replacement, and stops a pattern that runs longer than two seconds.

Regex Tester

Test regular expressions and see every match.

Open the Regex Tester

Questions

Add ? after the quantifier. .* takes as much as it can, while .*? takes as little as it can, so <b>.*?</b> stops at the first closing tag instead of the last.

A regex with the g or y flag remembers where its last match ended in lastIndex, and test() starts from there the next time. Testing the same string twice can therefore give true, then false. Create the regex inside the function, or reset lastIndex to 0.

Put a backslash before it: \. matches a dot, \( a bracket, and \\ a backslash. Inside a character class most symbols lose their meaning, so [.] also matches a dot.

Mostly, but not entirely. Lookbehind, named groups, and flags differ between engines, and some languages support features JavaScript lacks, such as possessive quantifiers. Test a pattern in the engine that will run it.

Sources

Missing a tool?

Tell us what you wanted to do. Your ideas decide which tools we build next.

Suggest a tool on wisemensoft.com