The exec()
method executes a search for a match in a specified string. Returns a result array, or null
.
JavaScript RegExp
objects are stateful when they have the global
or sticky
flags set (e.g. /foo/g
or /foo/y
). They store a lastIndex
from the previous match. Using this internally, exec()
can be used to iterate over multiple matches in a string of text (with capture groups), as opposed to getting just the matching strings with String.prototype.match()
.
A newer function has been proposed to simplify matching multiple parts of a string (with capture groups): String.prototype.matchAll()
.
If you are executing a match to find true
or false
, use RegExp.prototype.test()
method instead.
If you are executing a match to find its index position in the string, use String.prototype.search()
method instead.