String.prototype.match()
str.match(regexp) Returns:
Array | null · Added in vES3 · Updated March 13, 2026 · String Methods string regex match
The match() method searches a string using a regular expression and returns an array of matches, or null if no match is found.
Syntax
str.match(regexp)
Parameters
| Parameter | Type | Description |
|---|---|---|
| regexp | RegExp | A regular expression object. If a string is passed, it’s converted to a RegExp. |
Examples
Basic usage
const text = "The quick brown fox jumps over the lazy dog";
// Simple word search
console.log(text.match(/fox/));
// ['fox', index: 16, input: 'The quick brown fox...', groups: undefined]
// Find all occurrences (with /g flag)
console.log(text.match(/o/g));
// ['o', 'o', 'o', 'o']
// No match returns null
console.log(text.match(/cat/));
// null
Working with capture groups
const dates = "2026-03-09, 2025-12-25";
// Extract year, month, day using capture groups
const regex = /(\d{4})-(\d{2})-(\d{2})/;
const result = dates.match(regex);
console.log(result[0]); // '2026-03-09'
console.log(result[1]); // '2026'
console.log(result[2]); // '03'
console.log(result[3]); // '09'
Named capture groups
const log = "2026-03-09 14:30:45 [INFO] Server started";
// Named capture groups (ES2018+)
const pattern = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
const result = log.match(pattern);
console.log(result.groups.year); // '2026'
console.log(result.groups.month); // '03'
console.log(result.groups.day); // '09'
Common Patterns
Validating input
function isValidEmail(email) {
const pattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return pattern.test(email);
}
console.log(isValidEmail("user@example.com")); // true
console.log(isValidEmail("invalid")); // false
Extracting all URLs from text
const html = 'Visit https://jsguides.dev or http://example.com today!';
const urlRegex = /https?:\/\/[^\s]+/g;
const urls = html.match(urlRegex);
console.logurls);
// ['https://jsguides.dev', 'http://example.com']
match() vs matchAll()
| Feature | match() | matchAll() |
|---|---|---|
| Returns | Array or null | Iterator |
| Capture groups | Accessible | Accessible with indices |
| /g flag required | No | Yes (always) |