You will be able to test strings against regular expressions and extract matches using JavaScript RegExp methods such as test and exec, plus String methods like match and matchAll.
What it is
A regular expression is a pattern object. In JavaScript, you can create one with a literal such as /\d{4}/ or with new RegExp("\\d{4}"). Think of a RegExp as a search tool: test asks “is there a match?”, while exec asks “what is the next match?”.
RegExp.prototype.test returns a boolean. RegExp.prototype.exec returns a match array or null. String methods such as match, matchAll, replace, search, and split accept RegExp objects and return strings, arrays, or iterators.
Related terms: pattern, flags (g, i, m, s, u, y), capture group, lastIndex, and match object.
Why it matters
- Validate input quickly, such as dates, IDs, or email-like strings.
- Extract structured data from logs, user text, or API responses.
- Replace or normalize text without manual loops.
- Search case-insensitively or across multiple lines.
- Build simple parsers for formats like
YYYY-MM.
Syntax or steps
- Create a pattern:
const re = /\d{4}/;. - Test it:
re.test("2026-01")returnstrue. - Match once:
"2026-01".match(/\d{4}/)returns["2026"]. - Match all:
"2026-01".match(/\d+/g)returns["2026", "01"]. - Use
execwhen you need groups and position:/(\d{4})-(\d{2})/.exec("2026-01").
Example
const text = "Invoice 2026-01 total 42.50 USD";
const hasYear = /\d{4}/.test(text);
const year = text.match(/\d{4}/);
const numbers = text.match(/\d+(?:\.\d+)?/g);
const dateParts = /(\d{4})-(\d{2})/.exec(text);
console.log(hasYear);
console.log(year);
console.log(numbers);
console.log(dateParts);
Part-by-part:
testchecks whether the text contains four digits and returnstrue.matchwithoutgreturns the first match as an array, includingindexandinputproperties.matchwithgreturns all matched strings:["2026", "01", "42.50"].execreturns the full match and capture groups:["2026-01", "2026", "01", ...].- The pattern
\d+(?:\.\d+)?matches one or more digits, optionally followed by a dot and more digits.
Common mistakes
- Forgetting
gwhen usingmatchfor all matches. Withoutg,matchreturns only the first match. - Reusing a global or sticky RegExp with
testorexecwithout resettinglastIndex. The next call may start after the previous match. - Assuming
matchalways returns an array. It returnsnullwhen there is no match. - Using
matchwithgand expecting capture groups. It returns only matched strings; usematchAll, which requiresg, or anexecloop for groups.
When to use it
| Goal | Use | Why |
|---|---|---|
| Only yes or no | test | Fast boolean result. |
| First match with groups | exec or match without g | Returns groups, index, and input. |
| All simple matches | match with g | Returns an array of matched strings. |
| All matches with groups | matchAll | Returns an iterator of full match arrays. |
Use test for validation, match for simple extraction, exec when you need one detailed match, and matchAll when you need every match with groups.
Practice
Guided exercise: Given "user-42@example.com", extract the number.
const id = "user-42@example.com".match(/\d+/);
console.log(id[0]); // "42"
Challenge: Extract all years from "2024, 2025, 2026" and return an array. Hint: use the g flag.
const years = "2024, 2025, 2026".match(/\d{4}/g);
console.log(years); // ["2024", "2025", "2026"]
Quick check
Question: What does /\d{4}/.test("2026-01") return?
Answer: true, because the string contains four consecutive digits.
Summary
RegExp methods let you test and extract patterns from strings. Choose test for validation, match for simple extraction, and exec or matchAll when you need groups or multiple matches.