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.
Examples
Examples of incorrect code for this rule:
js
foo.replace(/a/g, bar);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);How to use
To enable this rule in the CLI or using the config file, you can use:
bash
oxlint --deny unicorn/prefer-string-replace-alljson
{
"rules": {
"unicorn/prefer-string-replace-all": "error"
}
}