Skip to content

Files

Latest commit

 

History

History
30 lines (18 loc) · 605 Bytes

prefer-regexp-exec.md

File metadata and controls

30 lines (18 loc) · 605 Bytes

Pattern: Missing use of RegExp#exec

Issue: -

Description

This rule enforces the use of the more performant way of applying regular expressions on strings. RegExp#exec is faster than String#match and both work the same when not using the /g flag.

Examples

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