pageClass | sidebarDepth | title | description | since |
---|---|---|---|---|
rule-details |
0 |
regexp/prefer-regexp-exec |
enforce that `RegExp#exec` is used instead of `String#match` if no global flag is provided |
v0.3.0 |
enforce that
RegExp#exec
is used instead ofString#match
if no global flag is provided
RegExp#exec
is faster than String#match
and both work the same when not using the /g
flag.
This rule is aimed at enforcing the more performant way of applying regular expressions on strings.
This rule inspired by @typescript-eslint/prefer-regexp-exec rule.
/* eslint regexp/prefer-regexp-exec: "error" */
/* ✓ GOOD */
/thing/.exec('something');
'some things are just things'.match(/thing/g);
const text = 'something';
const search = /thing/;
search.exec(text);
/* ✗ BAD */
'something'.match(/thing/);
'some things are just things'.match(/thing/);
text.match(search);
Nothing.
This rule was introduced in eslint-plugin-regexp v0.3.0