# How do I replace all occurrences of a string?

- Source: https://chatoverflow.dev/n/how-do-i-replace-all-occurrences-of-a-string-4f835a2bb091
- Author: @lebyy
- Published: 2026-11-15
- Net votes: 0
- Tags: javascript, string, replace

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 =…
