There's a recipe card taped to Mrs. Ratna's fridge door. It isn't for memorising, it's for a glance while cooking, and every recipe on it has been tried.
This lesson is that card for regex: everything you learned in this track, one row per pattern. Every row has been tried too: the right-hand column of each table is the real result of running its example in Node 24, mostly on chat, the Ratna's Kitchen PO chat that has been with us since the first lesson. How to use it: find what you need in the "Meaning" column, copy the example, then swap in your own text.
Characters and classes
| Pattern | Meaning | Tested |
|---|---|---|
tart |
letters as they are, case-sensitive | chat.match(/tart/g) → ['tart'] |
. |
any one character except a line break | chat.match(/j.r/g) → ['jar', 'jar', 'jar'] |
\. |
a real dot (special character escaped) | "3.14 0.5 314".match(/\d*\.\d+/g) → ['3.14', '0.5'] |
\d, \D |
a digit 0 to 9; not a digit | "PO-0412".match(/\D+/)[0] → 'PO-' |
\w, \W |
letters, digits and underscore ([A-Za-z0-9_]); anything else |
"[email protected]".match(/\W/g) → ['.', '@', '.'] |
\s, \S |
space, tab, line break; anything else | "mobile +44 7700-900-456".match(/\S+/g) → ['mobile', '+44', '7700-900-456'] |
[*_~] |
one character from the list | chat.match(/[*_~]/g).length → 6 |
[^\d.] |
one character not in the list | "$85.00".match(/[^\d.]+/)[0] → '$' |
[a-z] |
one character in the range | "TART 3 jars".match(/[a-z]+/g) → ['jars'] |
\p{L} + the u flag |
any letter, é included |
"café".match(/\p{L}+/gu) → ['café'], while /\w+/g → ['caf'] |
Without a backslash, a dot means "any character". So a decimal pattern that forgot its backslash also accepts "314", which has no dot at all: /\d*.\d+/.test("314") gives true, because the . eats the digit 1. For decimals like 3.14, write /\d*\.\d+/.
Quantifiers
| Pattern | Meaning | Tested |
|---|---|---|
* |
0 times or more | "jr jar jaar".match(/ja*r/g) → ['jr', 'jar', 'jaar'] |
+ |
1 time or more | chat.match(/\d+ dollars/g) → ['90 dollars', '255 dollars'] |
? |
0 or 1 time (optional) | "+44 7700 07700".match(/\+?\d+/g) → ['+44', '7700', '07700'] |
{4} |
exactly 4 times | chat.match(/PO-\d{4}/g) → ['PO-0412', 'PO-0413'] |
{10,} |
at least 10 times | chat.match(/\d{10,}/g) → ['1234567890', '07700900461'] |
{1,3} |
1 to 3 times | chat.match(/\$\d{1,3}/g) → ['$85', '$165', '$170'] |
+?, *? |
lazy: as few as possible | chat.match(/\*.+?\*/g) → ['*Tart*', '*Cheese Sticks*', '*Snowballs*'] |
Without the ?, \*.+\* is greedy and takes one long piece from *Tart* to *Snowballs*.
Anchors and word boundaries
| Pattern | Meaning | Tested |
|---|---|---|
^ |
start of the text; start of every line with the m flag |
chat.match(/^\[/gm).length → 6, without m → 1 |
$ |
end of the text; end of every line with the m flag |
chat.match(/PO-\d{4}$/gm) → ['PO-0412', 'PO-0413'], without m → ['PO-0413'] |
\b |
a word boundary | "tart, tarts".match(/\btart\b/g) → ['tart'], without \b → ['tart', 'tart'] |
Groups, choices and lookaround
| Pattern | Meaning | Tested |
|---|---|---|
(...) |
a capturing group | chat.match(/on (\d{2})\/(\d{2})/).slice(1) → ['25', '03'] |
(?:...) |
a group that doesn't capture | chat.match(/\d+ (?:dollars|bucks)\b/g) → ['90 dollars', '75 bucks', '255 dollars'] |
(?<day>...) |
a named group | chat.match(/on (?<day>\d{2})/).groups.day → '25' |
\1 |
the same text as group 1 | chat.match(/([*_~])[^*_~]+\1/g) → ['*Tart*', '*Cheese Sticks*', '*Snowballs*'] |
a|b |
or | chat.match(/tart|cheese/gi) → ['Tart', 'Cheese', 'tart', 'cheese', 'TART'] |
(?=...) |
must come after | chat.match(/\d+(?= dollars)/g) → ['90', '255'] |
(?!...) |
must not come after | "90 dollars 75 bucks".match(/\d+(?!\d| dollars)/g) → ['75'] |
(?<=...) |
must come before | chat.match(/(?<=\$)\d+/g) → ['85', '165', '170'] |
(?<!...) |
must not come before | "PO-0412 mobile 7700".match(/(?<!-)\b\d{4}\b/g) → ['7700'] |
Lookbehind (?<=...) and (?<!...) work in Safari since version 16.4.
Flags
| Flag | Meaning | Tested |
|---|---|---|
g |
every match, not just the first | chat.match(/PO-(\d{4})/) → ['PO-0412', '0412'], with g → ['PO-0412', 'PO-0413'] |
i |
ignore upper and lower case | chat.match(/tart/gi) → ['Tart', 'tart', 'TART'] |
m |
^ and $ work per line |
see the ^ and $ rows above |
s |
. also matches a line break |
/Code.+Dina/.test(chat) → false, with s → true |
u |
Unicode mode: \p{...} works, an emoji counts as one character |
/^.$/.test("😀") → false, with u → true |
y |
sticky: only tries exactly at lastIndex |
const y = /PO-\d{4}/y; y.lastIndex = 126; y.test(chat) → true |
Without the u flag, \p{L} isn't a property escape: "p{L}".match(/\p{L}/)[0] gives 'p{L}'.
JavaScript methods
| Method | Result | Tested |
|---|---|---|
re.test(text) |
true or false |
/PO-\d{4}/.test(chat) → true |
text.match(re) |
without g: the first match and its groups; with g: every matching text; none: null |
see the g flag row |
text.matchAll(re) |
every match with its groups (requires g) |
[...chat.matchAll(/PO-(\d{4})/g)].map((m) => m[1]) → ['0412', '0413'] |
re.exec(text) |
one match; with g it carries on from lastIndex |
/PO-(\d{4})/.exec(chat).index → 126 |
text.replace(re, x) |
a new text; replaceAll requires g |
"07700-900-123".replace(/-/g, "") → '07700900123' |
text.search(re) |
the position of the first match, or -1 |
chat.search(/PO-/) → 126 |
text.split(re) |
an array of pieces | "+44 7700-900-456".split(/[- ]/) → ['+44', '7700', '900', '456'] |
Ready-made patterns
| For | Pattern | Tested |
|---|---|---|
| Prices in words | /\d+ (?:dollars|bucks)\b/g |
on the chat → ['90 dollars', '75 bucks', '255 dollars'] |
| Dollar prices | /\$\d{1,3}(?:,\d{3})*(?:\.\d{2})?/g |
on the chat → ['$85', '$165', '$170'] |
| UK mobile numbers | /(?<!\d)(?:\+44 ?|0)7\d{3}[- ]?\d{3}[- ]?\d{3}(?!\d)/g |
on the chat → ['07700-900-123', '+44 7700-900-456', '07700900461'] |
| Tidy a phone into +44 | .replace(/[^\d+]/g, "").replace(/^0/, "+44") |
"07700-900-123" → '+447700900123' |
/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/ |
"[email protected]" → true, "[email protected]" → false |
|
| A name without ReDoS | /^\w+(?:\s\w+)*$/ |
"Cheese Sticks" → true |
| A real name, letters of any language | /^[\p{L}'’-]+(?:\s[\p{L}'’-]+)*$/u |
"Mary O'Brien" → true |
A YYYY-MM-DD date |
/^(\d{4})-(\d{2})-(\d{2})$/ |
"2026-03-25" → groups ['2026', '03', '25'] |
| Thousands commas | .replace(/\B(?=(\d{3})+(?!\d))/g, ",") |
"1250000" → '1,250,000' |
| Escape a user's input | RegExp.escape(word) |
"$85.00 (tart)" → '\$85\.00\x20\(tart\)' |
| Escape a user's input, Node 22 or older browsers | .replace(/[.*+?^${}()|[\]\\]/g, "\\$&") |
"$85.00 (tart)" → '\$85\.00 \(tart\)' |
The email pattern above checks the shape: [a-zA-Z0-9._%+-]+ is the part before the @, then the domain, a dot, and an ending of at least 2 letters.
⚠️ The whole track's mistakes, in one list
- A dot without a backslash.
.means any character; a real dot is written\.. - Forgetting
^and$when validating. Without them, one correct piece of the text is enough fortrue. - Reusing a
gregex fortest().lastIndexcarries over, so the result can alternate. - A quantifier inside a quantifier, like
(\w+)+or(\w+\s?)+, can hang (ReDoS) on a long input that fails at the end. - Copying a pattern to another language without checking. Python uses
(?P<name>...)and\1in the replacement text, PHP needs delimiters and has nogflag, Go has no lookaround and no backreference.
TL;DR: Cheat sheet = a quick reference where every row has been tested. The core you'll use most: \d (digit), \w (letters, digits and underscore), \s (space), . (anything, so a real dot is written \., as in /\d*\.\d+/ for 3.14), classes [...], quantifiers * + ? {n,m} (add ? for lazy), anchors ^ $ \b, groups ( ), (?: ), (?<name> ), choices |, and lookaround (?= ), (?! ), (?<= ), (?<! ). Flags: g every match, i ignore case, m per line, s dot includes line breaks, u Unicode mode for \p{...} and emoji, y sticky. The email pattern /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/ checks the shape of an email address. Always test with right and wrong inputs.
⚡ The core you'll use most
Write:
console.log("[email protected]".match(/\W/g)); // ['.', '@', '.']
Meaning: \d is a digit, \w a letter, digit or underscore ([A-Za-z0-9_]), \s a space. Their uppercase forms are the opposite: \W is anything but a letter, digit or underscore.
⚡ A real dot needs a backslash
Write:
console.log(/\d*.\d+/.test("314"));
Guess first: "314" has no dot at all. What is the result?
true. Without a backslash, the dot means any character, so it eats the digit 1. For decimals like 3.14, write /\d*\.\d+/.
Meaning: The mistake that runs through the whole track: . is any character, a real dot is \..
⚡ Flags change how it searches
Write:
console.log(chat.match(/PO-\d{4}$/gm)); // ['PO-0412', 'PO-0413']
Meaning: g every match, i ignore case, m ^ and $ per line, s the dot includes line breaks, u Unicode mode, y sticky. Always test a pattern with right and wrong inputs.