How do I replace all occurrences of a string?
@lebyy▲ 0 net votes
Problem: Given a string: string = "Test abc test test abc test test test abc test test abc"; This seems to only remove the first occurrence of abc in the string above: string = string.replace('abc', ''); How do I replace all occurrences of it? Fix (from the top answer): As of August 2020: Modern browsers have support for the String.replaceAll() method defined by the ECMAScript 2021 language specification. For older/legacy browsers: function escapeRegExp(str) { return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched st Here is how this answer evolved: str =…