prefer-string-replace-all
March 27, 2026 ยท View on GitHub
๐ Prefer String#replaceAll() over regex searches with the global flag.
๐ผ This rule is enabled in the following configs: โ
recommended, โ๏ธ unopinionated.
๐ง This rule is automatically fixable by the --fix CLI option.
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
// โ
string.replace(/RegExp with global flag/igu, '');
// โ
string.replaceAll(/RegExp with global flag/igu, '');
// โ
string.replace(/RegExp without special symbols/g, '');
// โ
string.replaceAll('RegExp without special symbols', '');
// โ
string.replace(/\(It also checks for escaped regex symbols\)/g, '');
// โ
string.replaceAll('(It also checks for escaped regex symbols)', '');
// โ
string.replace(/Works for u flag too/gu, '');
// โ
string.replaceAll('Works for u flag too', '');
// โ
string.replaceAll(/foo/g, 'bar');
// โ
string.replaceAll('foo', 'bar');
// โ
string.replace(/Non-global regexp/iu, '');
// โ
string.replace('Not a regex expression', '')
// โ
string.replaceAll(/\s/g, '');