You will be able to write a simple JavaScript regular expression, run it against a string, and explain what the pattern and flags do.
What it is
A RegExp object in JavaScript describes a pattern for matching text. Think of it as a tiny search engine for strings: you give it a pattern, and it can test whether text contains a match, find matches, or replace them.
Related terms include pattern, the rules you write; flags, options such as global or case-insensitive; match, a piece of text that fits the pattern; and capture group, a part of the pattern saved for later use.
Why it matters
- Extracting data from text, such as numbers, dates, or IDs.
- Validating simple input formats, such as phone numbers or ZIP codes.
- Replacing or cleaning text, such as masking digits or normalizing whitespace.
- Splitting strings on complex boundaries, such as multiple spaces or punctuation.
- Searching case-insensitively without manually lowercasing everything.
Syntax or steps
- Create a regex literal:
/pattern/flags. - Or create one from a string:
new RegExp("pattern", "flags"). - Use common flags:
gfor global,ifor case-insensitive,mfor multiline. - Run it with string methods such as
test,match,replace, orsplit.
Example
const text = "Order 123 shipped on 2024-05-01.";
const re = /\d+/g;
const matches = text.match(re);
console.log(matches); // ["123", "2024", "05", "01"]
const hasNumber = /\d/.test(text);
console.log(hasNumber); // true
const masked = text.replace(/\d+/g, "####");
console.log(masked); // "Order #### shipped on ####-##-##."
Part-by-part explanation
\d means “any digit.” The + means “one or more of the previous thing.” So \d+ matches a run of digits. The g flag tells match and replace to find every match, not just the first one.
text.match(re) returns an array of all digit runs. /\d/.test(text) returns true if at least one digit exists. text.replace(/\d+/g, "####") replaces each digit run with ####.
Common mistakes
- Forgetting the
gflag when you need all matches. Withoutg,matchmay return only the first match. - Reusing a global regex with
testorexecwithout resettinglastIndex. The regex can remember where it stopped. - Using special characters like
.,*,+, or?as literal text without escaping them. Use\.to match a period. - Trying to validate complex formats, such as email addresses, with a tiny regex. Keep validation simple or use a tested library.
When to use it
Use a regex when the rule is pattern-based. Use plain string methods when the rule is simple and exact.
| Task | Use regex when | Use string methods when |
|---|---|---|
| Find text | You need flexible patterns, such as digits or word boundaries. | You need an exact substring, such as includes("Order"). |
| Replace text | You need to replace all matching patterns, such as digits. | You need to replace one known phrase. |
| Validate input | The format has clear rules, such as ###-###-####. | The check is simple, such as non-empty or starts with a letter. |
Practice
Guided exercise: Write a regex that finds all words in "Buy 2 tickets for 3 people". Use /\b\w+\b/g with match.
Challenge: Replace every digit in "ID: 98765" with "X".
Expected output: "ID: XXXXX". Hint: use replace(/\d/g, "X").
Quick check
Question: What does /\d+/g match?
Answer: It matches one or more consecutive digits, and the g flag makes it find all such runs in the string.
Summary
A JavaScript regular expression is a pattern object that can test, find, and replace text. Start with small patterns, add flags deliberately, and use string methods when exact matching is enough.