What is a regex cheatsheet?
A regex cheatsheet is a compact reference for the symbols of regular expressions: what \d, \b, [^a-z], {2,7}, (?=...) or (?<name>...) mean, and how to combine them. Nobody remembers all of them, and the cost of a wrong guess is high, because a regex that is almost right quietly matches the wrong things. This page is the quick answer you keep open next to your editor.
It is written for JavaScript regular expressions, the flavour used by RegExp, String.match and String.replace in browsers, Node.js and Deno. The tables are grouped the way you build a pattern: normal characters, whitespace, character sets, characters that must be escaped, quantifiers, boundaries, matching with lookahead and lookbehind, and grouping with capturing. Most of it also works in Python, Java, PHP or Go, but not everything: lookbehind, named groups and Unicode classes are the usual differences, so check the manual of the language where the pattern will really run.
The reference is not enough on its own. A pattern only becomes trustworthy when you see it match real text, so the page links to the regex tester, where you can paste the pattern, tick the flags and check every match and group.
How to build a regex from the cheatsheet
- Write in plain words what you want to match: "two digits, an optional parenthesis, a dash, four digits".
- Look up each piece in the tables. Digits are
\d, an optional character takes?, and a literal dash is just-outside a set. - Put the quantifier right after the piece it repeats:
\d{2}is two digits,\d+is one or more,\d*is zero or more. - Escape the characters that have a special meaning when you want them literally:
. ^ $ | \ / ( ) [ ] { }. Write\.for a real dot and\(for a real parenthesis. - Anchor the pattern with
^and$when the whole text must fit, or with\bwhen you look for a whole word. - Open the regex tester, paste a text with cases that should match and cases that should not, and check the matches. Adjust the flags:
gfor all matches,ito ignore case,mfor line by line. - Only then copy the literal
/pattern/flagsinto your code.
Examples
A Brazilian phone number
\(?\d{2}\)?\s?9?\d{4}-?\d{4} reads as: an optional (, two digits, an optional ), an optional space, an optional 9, four digits, an optional dash and four digits. On (11) 91234-5678 e 21 3456-7890 it finds both (11) 91234-5678 and 21 3456-7890.
Swap the date order with groups
Capturing groups let you reuse pieces in a replacement:
'2026-11-21'.replace(/(\d{4})-(\d{2})-(\d{2})/, '$3/$2/$1'); // 21/11/2026Each pair of parentheses gets a number from 1, and $1 is its content. With named groups the same thing is $<day>/$<month>/$<year>.
A password rule with lookahead
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}$ demands a digit, a lowercase letter, an uppercase letter and at least 8 characters, in any order. Each (?=...) only looks ahead and consumes nothing. Abcdefg1 passes; abcdefg1 fails for lack of an uppercase letter.
Why \w does not like accents
\w means [A-Za-z0-9_], so on café ação the pattern \w+ returns caf, a and o, cutting each word at the accent. With the u flag, \p{L}+ returns café and ação. The same limit affects \b, which relies on \w.
Common mistakes and how to fix them
- An unescaped dot matches anything:
1.5also matches125. Write1\.5for the literal number. - Greedy quantifiers eat too much:
<.+>on<a><b>returns the whole text. Use<.+?>or<[^>]+>to get<a>and<b>. ^and$only see the whole text: to anchor each line, add themflag.- Inside [ ] most symbols are literal:
[a-z.-]already has a literal dot, and a-at the end is not a range. A^negates only when it comes first. - The
gflag andtest(): a regex object withgremembers where it stopped, so callingtesttwice on the same text can returntrueand thenfalse. Create the regex again or dropg. - Forgetting the
/in a literal: inside/.../a slash must be escaped as\/; withnew RegExp('...')it does not.
Frequently asked questions
What is the difference between \d, \w and \s?
\d is a digit, \w is a letter without accent, digit or underscore, and \s is a space, tab or line break. Their uppercase forms (\D, \W, \S) mean the opposite.
What does the ? mean in a regex?
After a piece, ? makes it optional (zero or one time). After another quantifier, as in .*?, it makes it lazy, so it takes the least text possible. Inside a group, (?:...), (?=...) or (?<name>...), it starts a special kind of group.
How do I match a whole word?
Surround it with \b: \bcat\b finds cat but not category. Remember that \b follows \w, so a word that starts or ends with an accented letter needs another approach, such as lookarounds with \p{L} and the u flag.
Do regexes work the same in every language?
The basics do. Lookbehind, named group syntax, flags and Unicode support vary, so test where the pattern will run. Here you can check JavaScript; for text that is easier to compare than to match, the text diff may be a better tool.
How do I turn text into a clean identifier with a regex?
Replace everything that is not a letter or digit with a dash, trim the dashes and lowercase it. The slugify tool does this for you.
Using regex in JavaScript
JavaScript
The most useful methods, with real results:
/\d{4}-\d{2}/.test('release 2026-11'); // true
'a1b22'.match(/\d+/g); // ['1', '22']
[...'a1b22'.matchAll(/\d+/g)].map(m => m.index); // [1, 3]
'a, b;c'.split(/[,;]\s*/); // ['a', 'b', 'c']
'foo bar'.replaceAll(/o/g, '0'); // 'f00 bar'
const m = 'x 2026-11-21'.match(/(?<year>\d{4})-(?<month>\d{2})/);
m.groups.year; // '2026'
m.groups.month; // '11'