Favorites

JavaScript RegExp.prototype.exec() Method

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
RegExp.prototype.exec()
JavaScript RegExp Instance methodThis method is supported by all modern browsers.
Updated 28th November 2026

The exec() method searches a string for a regular expression match and returns details of the match, including capture groups.

Description

The exec() method executes a search and returns a result array: the full match first, then each capture group, along with index (the position) and groups (named groups). It returns null if there is no match.

With the g flag, each call continues from lastIndex, so it can be called in a loop to find every match. String.prototype.matchAll() is usually a simpler way to do that.

Without the g or y flag, exec() always starts from the beginning and returns the same first match each time, so a loop around it would never end. The d flag adds an indices property with the start and end positions of each group, which is useful for highlighting matches.

Syntax

regexp.exec(string)

Parameters

  • stringThe text to search.

Return Value

A match array with capture groups, index and groups, or null if there is no match.

Example

const re = /(?<key>\w+)=(?<value>\d+)/g;
const text = 'a=1 b=22';
let m;
while ((m = re.exec(text)) !== null) {
  console.log(m.groups.key, m.groups.value, 'at', m.index);
}

Browser Support

The following information will show you the current browser support for the RegExp.prototype.exec() method. Hover over a browser icon to see the version that first introduced support for this method.

This method is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee