Skip to content
Modern JavaScriptChapter 44 / 47Intermediate5 min read

Regular Expressions

Learn what JavaScript regex is, how patterns and flags work, and how to use test, match and replace, with email and phone validation examples.

On this page

1. What are regular expressions?

In JavaScript you can create a regex in two ways: a literal /abc/i or the constructor new RegExp("abc", "i"). The literal is more common, and the constructor is for when you need to build the pattern at runtime.

2. Why do they matter?

Imagine you need to check that a string has only 4 to 6 digits. Without regex, you would have to write loops, if statements and character checks. Regex does this in one line.

Manual string codeWith regex
A loop and an if on every characterOne pattern describes the whole rule
A new rule means a new functionChange the pattern a little
Finding all matches is a lot of workAll matches at once with the g flag
Split and join to replacereplace or replaceAll in one call

3. Where are they used?

Use caseExample
Form validationCheck email, phone, postal code
SearchFind a word in text, ignoring case
CleanupRemove extra spaces, build a slug
ParsingPull parts out of log lines or dates
EditorsVS Code find and replace supports regex

4. How do they work?

The regex engine reads the string from left to right and tries to match the pattern at every position.

Regex matching flow
Create the pattern and flags
Call a method on the string
The engine tries a match from each position
If it matches you get a result, otherwise null or false

Common building blocks

PatternMeaning
\dAny digit from 0 to 9
\wLetter, digit or underscore
\sSpace, tab, newline
.Any character except a newline
[abc]One of a, b or c
^ and $Start and end of the string
+, *, ?One or more, zero or more, optional
{2,4}2 to 4 times
a|ba or b
( )A group, captures the value

Flags

FlagJob
gAll matches, not just the first
iCase-insensitive
mMultiline, ^ and $ work on every line
uUnicode support (emoji, Urdu text)

5. Code example

test, match and replace

js
const phone = /^03\d{2}-?\d{7}$/;
console.log(phone.test("0300-1234567"));
console.log(phone.test("12345"));

const text = "Order 12 and order 45 have shipped";
console.log(text.match(/\d+/g));

const slug = "  Learn JS   Regex  ".trim().toLowerCase().replace(/\s+/g, "-");
console.log(slug);

Named groups and matchAll

js
const datePattern = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
const result = "2026-11-01".match(datePattern);
console.log(result.groups.year, result.groups.month);

const log = "user=ali id=7, user=sara id=9";
for (const m of log.matchAll(/user=(\w+) id=(\d+)/g)) {
  console.log(m[1], m[2]);
}

How does the code work?

ItemMeaning
^03\d{2}The string starts with 03, then 2 digits
-?The dash is optional
\d{7}$Exactly 7 digits at the end
.test()Returns true or false
.match(/\d+/g)An array of all the numbers: ["12", "45"]
\s+ with gReplaces every group of spaces with a dash
(?<year>...)A named group, available as result.groups.year
matchAllWith the g flag, gives every match and its groups

Interview tip

A regex with the g flag remembers lastIndex. If you call .test() again and again on the same regex, the results keep changing. Do not use the g flag for validation.

6. Common mistakes

Mistake: Forgetting ^ and $ in a validation pattern

Why? Without anchors, the pattern matches anywhere in the string, so even abc12345xyz passes.

Better: To check the whole string, start the pattern with ^ and end it with $.

Mistake: Not escaping special characters

Why? A dot means any character, so a pattern like /1.5/ also matches 125.

Better: Use a backslash for a literal dot, like /1\.5/.

Mistake: Writing a single backslash in new RegExp

Why? Inside a string, the backslash is itself an escape character, so \d becomes just d.

Better: Write a double backslash in the constructor, or use a regex literal wherever possible.

Mistake: Trying to write a perfect regex for things like email

Why? A very complex pattern is hard to read and maintain, and it still does not cover every case.

Better: Do a basic check with a simple pattern, and do the real verification on the server or with a confirmation email.

7. What to learn before and after

Final quick revision

PointDetail
RegexA pattern for text
How to create/pattern/flags or new RegExp()
Check.test() returns true or false
Extract.match(), .matchAll()
Replace.replace(), .replaceAll()
Anchors^ and $ for the whole string