Pattern: Global string replacement with replace()
Issue: -
The replaceAll()
method provides a safer and more efficient way to replace all occurrences of a pattern compared to using replace()
with a global regex. It's clearer in intent and doesn't require regex escape handling for string patterns.
Example of incorrect code:
string.replace(/foo/g, 'bar');
string.replace(new RegExp(pattern, 'g'), 'bar');
Example of correct code:
string.replaceAll('foo', 'bar');
string.replaceAll(/foo/, 'bar');