Skip to content

Commit

Permalink
docs(eslint-plugin): update justification for prefer-regexp-exec (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
rrthomas committed May 15, 2021
1 parent df7a0d6 commit b85261c
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions packages/eslint-plugin/docs/rules/prefer-regexp-exec.md
@@ -1,18 +1,16 @@
# Enforce that `RegExp#exec` is used instead of `String#match` if no global flag is provided (`prefer-regexp-exec`)

`RegExp#exec` is faster than `String#match` and both work the same when not using the `/g` flag.
As `String#match` is defined to be the same as `RegExp#exec` when the regular expression does not include the `g` flag, prefer a consistent usage.

## Rule Details

This rule is aimed at enforcing the more performant way of applying regular expressions on strings.
This rule is aimed at enforcing a consistent way to apply regular expressions to strings.

From [`String#match` on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match):

> If the regular expression does not include the g flag, returns the same result as `RegExp.exec()`.
From [Stack Overflow](https://stackoverflow.com/questions/9214754/what-is-the-difference-between-regexp-s-exec-function-and-string-s-match-fun)

> `RegExp.prototype.exec` is a lot faster than `String.prototype.match`, but that’s because they are not exactly the same thing, they are different.
`RegExp#exec` may also be slightly faster than `String#match`; this is the reason to choose it as the preferred usage.

Examples of **incorrect** code for this rule:

Expand Down

0 comments on commit b85261c

Please sign in to comment.