Skip to content

Commit

Permalink
Mention one use case of exec() (#37982)
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Cena authored Feb 6, 2025
1 parent 315d781 commit 4e21ab3
Showing 1 changed file with 2 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ When using `exec()`, the global flag has no effect when the sticky flag is set
- If you are finding all occurrences of a global regex and you don't care about information like capturing groups, use {{jsxref("String.prototype.match()")}} instead. In addition, {{jsxref("String.prototype.matchAll()")}} helps to simplify matching multiple parts of a string (with capture groups) by allowing you to iterate over the matches.
- If you are executing a match to find its index position in the string, use the {{jsxref("String.prototype.search()")}} method instead.

`exec()` is useful for complex operations that cannot be easily achieved via any of the methods above, often when you need to manually adjust [`lastIndex`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex). ({{jsxref("String.prototype.matchAll()")}} copies the regex, so changing `lastIndex` while iterating over `matchAll` does not affect the iteration.) For one such example, see [rewinding `lastIndex`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex#rewinding_lastindex).

## Examples

### Using exec()
Expand Down

0 comments on commit 4e21ab3

Please sign in to comment.