Regex cheat sheet for JavaScript, with patterns you can test right away
Classes, quantifiers, anchors, groups and flags on one page, plus five starter patterns to try in the free AyeTab RegExp Tester.
By ClaudeGPT Si Newsroom · Editorial team
· 3 min read
This regex cheat sheet for JavaScript covers the tokens you will use most: character classes, quantifiers, anchors, groups and flags, each with a one-line example. Keep it open next to the free AyeTab RegExp Tester, which runs the same JavaScript engine, and try each line as you read it.
Characters and classes
| Token | Matches | Example |
|---|---|---|
. |
Any character except a line break | c.t finds cat, cot |
\d / \D |
A digit / not a digit | \d\d finds 42 |
\w / \W |
Letter, digit or _ / anything else |
\w+ finds user_1 |
\s / \S |
Whitespace / not whitespace | a\sb finds "a b" |
[abc] |
One of the listed characters | gr[ae]y |
[^abc] |
Any character not listed | [^0-9] |
[a-z] |
A range | [A-F0-9] for hex |
\. |
A literal dot (escape special characters) | example\.com |
Quantifiers
| Token | Meaning |
|---|---|
* |
Zero or more |
+ |
One or more |
? |
Zero or one (optional) |
{3} |
Exactly three |
{2,5} |
Between two and five |
+? or *? |
Lazy: as few as possible |
Quantifiers are greedy by default. <.+> on <b>hi</b> matches the whole string; <.+?> stops at the first closing bracket.
Anchors and boundaries
^start of the text (or of each line with the m flag)$end of the text (or of each line with the m flag)\ba word boundary, so\bcat\bskips "concatenate"
Groups and lookarounds
(abc)capturing group, available as$1in a replacement(?:abc)group without capturing(?<year>\d{4})named groupa|beither sidex(?=y)x followed by y;x(?!y)x not followed by y(?<=y)xx preceded by y;(?<!y)xx not preceded by y
Flags
| Flag | Effect |
|---|---|
g |
Find all matches, not only the first |
i |
Ignore case |
m |
^ and $ work per line |
s |
. also matches line breaks |
u |
Unicode mode |
y |
Sticky: match only at the current position |
The AyeTab tester has buttons for g, i and m, the three you reach for most.
Reading a pattern from left to right
Cheat sheets help most when you can read a whole pattern, not just single tokens. Take ^\d{4}-\d{2}-\d{2}$. The caret pins the match to the start of the text. Then come four digits, a hyphen, two digits, another hyphen and two more digits. The dollar sign pins the end, so nothing extra can sit before or after the date.
Read every pattern this way, one token at a time, and most surprises disappear. When a pattern still behaves oddly, the cause is usually one of three things: a special character that needed a backslash, a greedy quantifier that ran too far, or a flag that is on in one place and off in another.
One JavaScript detail catches people out. Inside a regex literal such as /\d+/ you write a single backslash, but inside a string passed to new RegExp("\\d+") you need two, because the string itself uses the backslash as an escape. Testers take the pattern as you would write it in a literal.
Five patterns to start from
- Digits only:
^\d+$ - Hex colour:
^#?[0-9a-fA-F]{6}$ - ISO date:
^\d{4}-\d{2}-\d{2}$ - Leading or trailing spaces:
^\s+|\s+$ - Simple email check:
^[^\s@]+@[^\s@]+\.[^\s@]+$
Paste each into the tester with a few lines that should and should not match before you trust it.
Where to try them
The AyeTab RegExp Tester is free, needs no account and highlights matches in the browser without uploading your text. If you use it daily, the AyeTab Chrome extension keeps it one Ctrl+K away on every new tab. For a cheat sheet built into the tester itself, or for flavors other than JavaScript, RegExr and regex101 are good second stops.