Strings
Regular expressions /…/
A mini-language of patterns: search text not for an exact string but for "something that looks like a phone number".
Why it exists
includes and replaceAll look for exact text. But you often need not a particular string, but any string of a certain shape: any phone number, any number, any hashtag, "five digits and nothing else". Without regexes that means loops over characters with a pile of ifs — dozens of lines per check. A regular expression describes the shape of a string in one short line, and the JavaScript engine does the scanning.
Where it's used
- Validating form input: a ZIP code
/^\d{5}$/, a username/^[a-z0-9_]{3,12}$/, a time/^\d{2}:\d{2}$/. - Cleaning data: keep only the digits of a phone number, collapse extra spaces with
/\s+/g. - Searching text: hashtags and mentions in posts, numbers in a report, highlighting search results.
- Page addresses (slugs) from titles, picking apart strings like
2026-10-15with groups.
Syntax
/pattern/flags
pattern.test(string)
string.match(/pattern/g)
string.replace(/pattern/g, replacement)Examples
const zip = /^\d{5}$/;
zip.test("90210"); // → true
zip.test("9021"); // → false
"3 cats and 12 mice".match(/\d+/g); // → ["3", "12"]
"+1 (555) 123-4567".replace(/\D/g, ""); // → "15551234567"const nick = /^[a-z0-9_]{3,12}$/;
nick.test("byte_007"); // → true
nick.test("ab"); // → false — shorter than three
nick.test("byte-007"); // → false — no hyphens allowedconst date = "2026-10-15";
date.replace(/(\d{4})-(\d{2})-(\d{2})/, "$2/$3/$1"); // → "10/15/2026"
date.match(/(\d{4})-(\d{2})/)[1]; // → "2026""fish, mouse, cream".split(/,\s*/); // → ["fish", "mouse", "cream"]split understands regexes too: a comma and any number of spaces after it"café au lait".match(/\w+/g); // → ["caf", "au", "lait"]
"café au lait".match(/\p{L}+/gu); // → ["café", "au", "lait"]\p{L} with u: \w doesn't see themResults after // → aren't just written down — an automatic check runs and verifies them before every site update.
Why it works this way
A regex is written between slashes: /cat/. Letters and digits mean themselves, special characters are commands:
- classes:
\d— a digit,\w— a Latin letter, digit or_,\s— whitespace,[aeiou]— any one of those listed,.— any character; the capitals\D,\W,\Smean "anything but"; - quantifiers after a piece:
+— one or more,*— zero or more,?— optional,{2,4}— two to four; - anchors:
^— start of the string,$— its end; - groups
( )remember part of the match; in replace it's available as$1,$2…
The engine tries to fit the pattern against the text from each position in turn: from the first character, then the second, and so on, until it finds a match. That's why, without ^ and $, a pattern is found anywhere inside the string.
Flags go after the closing slash: g — find every match, i — ignore case, u — full Unicode (for example, \p{L} is any letter of any alphabet).
The syntax came from the Unix tool grep and the Perl language, which is why it's nearly the same in JavaScript, Python, Java and code editors. If the pattern is built from a variable, use the constructor new RegExp("text", "gi").
Traps
How it differs from…
replace()andreplaceAll()A string in replace/replaceAll looks for exact text — simpler and faster. You need a regex when you're looking for a shape of text: any digits, any whitespace, any word in any case.
split()andjoin()split(" ") cuts on an exact separator and leaves empty pieces on double spaces.
split(/\s+/)cuts on any run of whitespace.
When to pick something else
If includes, startsWith, endsWith or a string replaceAll will do, use them: they're clearer. Don't parse HTML with regexes (nested tags are beyond them — there's DOMParser), nor JSON (JSON.parse), nor URLs (new URL(...)). Don't try to fully validate an email with one regex: "there's an @ and a dot after it" is enough, and the real check is a confirmation email.