JavaScript (ES2026) reference · String Methods
JavaScript String match() Method
Match the string against a regular expression and return the matches (all of them with the g flag) or null.
JavaScript (ES2026) Since ES3
Syntax
string.match(regexp)
Parameters
| Parameter | Description |
|---|---|
regexp |
A regular expression (a string is converted to one). |
Return value
An array of matches, or null.
Examples
const text = "Order 42 shipped, order 7 pending";
console.log(text.match(/\d+/g));
console.log(text.match(/order (\d+)/i)[1]);
Output
[ '42', '7' ] 42
const m = "nothing here".match(/\d+/g);
console.log(m, m ?? []);
Output
null []
Version history
| Version | Change |
|---|---|
| JavaScript ES3 | Added |
Related
string.matchAll()· Return an iterator of every regular-expression match, including capture groups. The regex must have the g flag.string.search()· Return the index of the first regular-expression match, or -1 if there is none.string.replace()· Replace the first match of a string or regex (every match with the g flag) with new text or a function's result.- JavaScript Strings · tutorial
- JavaScript String Methods · tutorial