Skip to content

Latest commit

 

History

History
24 lines (15 loc) · 640 Bytes

prefer-replace-all.md

File metadata and controls

24 lines (15 loc) · 640 Bytes

Prefer String.prototype.replaceAll over regex searches with the global flag.

As the replaceAll method is being added to strings, there is no reason to use expensive regex searches when the search string is a string-literal.

This rule is fixable.

Fail

str.replace(/This has no special regex symbols/g, 'something');
str.replace(/\(It also checks for escaped regex symbols\)/g, 'something');

Pass

str.replace(/Non-literal characters .*/g, 'something');
str.replace(/Extra flags/gi, 'something');
str.replace('Not a regex expression', 'something')
str.replaceAll('literal characters only', 'something');