Mrs. Ratna takes pre-orders (PO) for Eid cookies in a WhatsApp group. She asks her little brother to write down every phone number in that group. She doesn't list the numbers, because she doesn't know them yet herself. She only describes them: "the ones that start with 07 or +44, then more digits". He knows right away what to look for, even though each number is different.
A regex, short for regular expression, is a pattern: a description of text, written in a form a computer understands. What Mrs. Ratna said is a description of a phone number. If you write that description as a regex, the computer can do her brother's job and find every piece of text that fits. Ctrl+F (the search box in a browser) can only find one exact piece of writing, while a regex finds every piece of text that fits its description, such as "every phone number".
Throughout this track we use one text, the Ratna's Kitchen PO chat: what was written in the WhatsApp group where Mrs. Ratna takes cookie orders. It is as messy as a real chat: names, phone numbers written three ways, prices written as $85, 90 dollars and 75 bucks (bucks is everyday slang for dollars), dates and order codes. The numbers are written the way the group writes them: 07.15 is a time and 12/03/26 a date, 12 March 2026. Every lesson takes more data from this chat.
[12/03/26 07.15] Ratna (Admin): Eid cookie PO is open! *Tart* $85/jar, *Cheese Sticks* 90 dollars, *Snowballs* 75 bucks. Code PO-0412
[12/03/26 07.17] Dina: sis, 2 jars of tarts please. WhatsApp me 07700-900-123
[12/03/26 07.20] Bayu: I'll take cheese sticks 1, snowballs 1. How much? mobile +44 7700-900-456
[12/03/26 07.22] Ratna (Admin): @Bayu total $165 please, transfer to account 1234567890 under Ratna
[12/03/26 07.25] Sari: TART 3 jars = 255 dollars right? my no. 07700900461, deliver on 25/03
[12/03/26 07.31] Dina: transfer $170 done, receipt to [email protected]. Code PO-0413
Your first regex in JavaScript
In JavaScript we keep the chat above as text in a variable called chat. Then we ask two things: whether a pattern is in the chat, and which text matches the pattern:
const chat = `[12/03/26 07.15] Ratna (Admin): ...`; // the 6 chat lines above
console.log(/PO-0413/.test(chat)); // true
console.log(/PO-0414/.test(chat)); // false
console.log(chat.match(/tart/)); // ['tart'], index: 172
/PO-0413/is a regex literal: a regex written directly in the code, with its pattern between two slashes. The slashes only wrap the pattern. They are not part of it.- Plain letters and digits in a pattern match themselves, character by character. The pattern
PO-0413matches the textPO-0413. pattern.test(text)answerstrueif the pattern is found anywhere in the text, andfalseif it isn't. It fits a question like "is it there?".text.match(pattern)returns the text that matched, inside an array. Without a flag (explained in the section The g and i flags),match()stops at the first match and also gives itsindex: the position of that text inchat, counted from 0.
In this example, the first match is tart at index 172. It is a piece of the word tarts in Dina's message. Tart in the first message is skipped because its T is uppercase. A regex looks for pieces of text, not whole words. The Anchors lesson shows how to limit a search to whole words.
How the regex engine searches
The regex engine is the part of JavaScript that runs the search. This is how it works:
- The engine starts at the first character of the text and compares the pattern with the text, character by character.
- As soon as a character differs, the engine moves one character to the right and tries again.
- The first position where the whole pattern fits is the result.
A small example: the pattern ar in the text Tart.
| Start at index | Compared | Result |
|---|---|---|
| 0 | T with a |
different, move on |
| 1 | a with a, then r with r |
match: ar at index 1 |
The g and i flags
A flag is a letter written after the closing slash. A flag changes how the search runs, not the pattern. The chat has three spellings: Tart, tarts and TART. Before you read the comments in this code, guess: how many does /tart/g find?
console.log(chat.match(/tart/g)); // ['tart']
console.log(chat.match(/tart/gi)); // ['Tart', 'tart', 'TART']
console.log(chat.match(/tart/i)); // ['Tart'], index: 56
g(global): find every match, not only the first. Withg,match()returns an array of all the matches, withoutindex.i(ignore case): uppercase and lowercase count as the same.- Flags can be combined:
/tart/gi.
The answer to the guess: 1 match. A regex treats uppercase and lowercase as different, so /tart/g finds only the tart inside tarts. Tart and TART are found too once the i flag is added.
When to use regex, and when not to
Regex is used most for format validation: checking whether an email, a phone number or a postal code typed into a form follows the expected pattern. Regex is also used for:
- Pulling data out of text: every price, every order code, every phone number in the chat.
- Find & replace: hiding phone numbers, changing a date format.
- Searching in an editor: VS Code's Find box has a
.*button to search with a regex.
When you are looking for one fixed piece of text, you don't need a regex. chat.includes("PO-0413") also answers true, and it is easier to read. To read HTML or JSON, use the tool made for it (JSON.parse, DOMParser). Their content is nested (for example, tags inside tags), and a regex easily misreads text like that.
⚠️ Why do you get false, or an error?
- Forgetting about case.
/po-0413/.test(chat)givesfalse, because the chat saysPO-0413, in uppercase. Add theiflag when uppercase or lowercase doesn't matter. match()with no result returnsnull, not an empty array. The word brownies is not in the chat, sochat.match(/brownies/g).lengthgives an error right away:TypeError: Cannot read properties of null (reading 'length'). Write(chat.match(/brownies/g) || []).length. The|| []part replacesnullwith an empty array, so the result is0.- Forgetting
g. Withoutg,match()stops at the first match. If you need all of them, addg.
TL;DR: A regex (regular expression) is a pattern that describes the text you are looking for. In JavaScript a regex is written /pattern/flags. Plain letters and digits in a pattern match themselves, and what a regex finds are pieces of text, not whole words. pattern.test(text) answers true or false: the pattern is in the text or it isn't. text.match(pattern) returns an array with the text that matched, or null when nothing matches. A flag is a letter after the closing slash: g finds every match, and i counts uppercase and lowercase as the same. The most common use is format validation, which means checking typed values like email addresses and phone numbers. To find one fixed piece of text, includes() is enough.
⚡ A regex describes text
Meaning: A regex (regular expression) is a pattern: a description of text, written so that a computer can search for it. Ctrl+F (the search box in a browser) finds only one exact piece of writing. A regex finds every piece of text that fits a description, such as a phone number: "the ones that start with 07 or +44, then more digits". Its most common use is format validation, which means checking typed values like email addresses and phone numbers in a form.
⚡ Is it there, and what matches
Write:
console.log(/PO-0413/.test(chat)); // true
console.log(chat.match(/tart/));
Guess first: chat holds the text of a cookie-order chat. In it there is Tart, then tarts, then TART. Letters in a pattern match only the exact same letters. Which one does the pattern /tart/ find first?
The tart inside tarts, at index 172 (its position in the text, counted from 0). Tart is skipped because a regex treats uppercase and lowercase as different. A regex also looks for pieces of text, not whole words.
Meaning: A regex pattern is written between two slashes, like /PO-0413/. pattern.test(text) answers true if the pattern is in the text, and false if it isn't. text.match(pattern) returns an array with the first text that matched, or null when nothing matches.
⚡ The g and i flags
Write:
console.log(chat.match(/tart/gi)); // ['Tart', 'tart', 'TART']
Meaning: A flag is a letter after the closing slash, and it changes how the search runs. g finds every match, not only the first. i counts uppercase and lowercase as the same.