Skip to content

Latest commit

 

History

History
61 lines (38 loc) · 1.68 KB

prefer-regexp-exec.md

File metadata and controls

61 lines (38 loc) · 1.68 KB
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

regexp/prefer-regexp-exec

enforce that RegExp#exec is used instead of String#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.

📖 Rule Details

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);

🔧 Options

Nothing.

📚 Further reading

🚀 Version

This rule was introduced in eslint-plugin-regexp v0.3.0

🔍 Implementation