JavaScript RegExp.prototype.exec() Method
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
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.
Desktop





Tablets & Mobile





