RegExp.prototype.exec()

Executes a search and returns a match array, or null if no match.

Since ES3 Spec ↗

Syntax

regexp.exec(str)

Returns

array — A match array with `index`, `input`, and `groups`, or `null`.

Examples

const m = /(\d+)-(\d+)/.exec("12-34");
console.log(m[0], m[1], m[2]);
Output
12-34 12 34
const re = /\w+/g;
let m;
while ((m = re.exec("a b c")) !== null) {
  console.log(m[0]);
}
Output
a
b
c

Notes

- With the `g` flag, repeated calls iterate matches using `lastIndex`. - Returns `null` when there is no match. - Capture groups appear at indices 1..n; named groups in `.groups`.

Browser & runtime support

EnvironmentSince version
chrome 1.0
firefox 1.0
safari 1.0
edge 12
node 0.10

See also