unicorn/prefer-string-replace-all
Require replaceAll() instead of replace() with a global regex
fixable
replace() only replaces every match if the regex has the g flag, and one letter at the end of a pattern is easy to miss. replaceAll() says it in the name.
The rule also reports a regex passed to replaceAll() when it only matches one fixed string, because a plain string does the same thing and doesn't need escaping.
--fix renames the method, and turns the regex into a string when it matches one fixed string.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"unicorn/prefer-string-replace-all" = "error" # or "warn"Examples#
example.js2 problems
const slug = title.replacePrefer String#replaceAll() over String#replace() when using a regex with the global flag.(/\s+/g, "-");
const path = name.replaceAll(/\./gThis pattern can be replaced with .., "/");example.jsNo problems
const slug = title.replaceAll(/\s+/g, "-");
const path = name.replaceAll(".", "/");
const first = title.replace(/\s+/, "-");