unicorn/prefer-string-replace-all Pedantic
What it does
Prefers String#replaceAll()
over String#replace()
when using a regex with the global flag.
Why is this bad?
The String#replaceAll()
method is both faster and safer as you don't have to use a regex and remember to escape it if the string is not a literal. And when used with a regex, it makes the intent clearer.
Example
Examples of incorrect code for this rule:
js
array.reduceRight(reducer, initialValue);
Examples of correct code for this rule:
js
foo.replace(/a/, bar);
foo.replaceAll(/a/, bar);
const pattern = "not-a-regexp";
foo.replace(pattern, bar);