"Replace All" in JavaScript: From Ancient Workarounds to Modern Standards
Imagine a scenario: you're writing a chatbot, and a user sent a message with a typo "привеТТ". You need to replace all occurrences of "Т" with "т". Or you're parsing a CSV file and want to remove all extra spaces. Sounds simple? In JavaScript, until recently, this was a real headache.
The question "How do I replace all occurrences of a string?" is one of the most popular on Stack Overflow. Thousands of developers have stumbled over this seemingly elementary task. Why? Because the good old replace() by default replaces only the first match. The trick? You need to use a regular expression with the g flag. But in 2025, we have a much more elegant solution.
In this article, we'll cover all the current ways to replace all occurrences of a substring in JavaScript: from classics to modern standards. You'll learn which method to choose for different tasks and why replaceAll is your new best friend.
Why doesn't the old replace() work "out of the box"?
Let's start with the disappointment that every beginner has faced:
let text = "I love JavaScript, JavaScript is awesome!";let newText = text.replace("JavaScript", "Python");console.log(newText); // Output: "I love Python, JavaScript is awesome!"See? Only the first occurrence was replaced. The String.prototype.replace() method was historically designed this way. If you pass it a string (not a regular expression), it finds and replaces the first match. For many languages (Python, Java), this was a shock. But JavaScript lived with this limitation for a long time.
By the way, if you are looking for the answer to the question "how do I replace all occurrences of a string in JavaScript", know that until 2021, the only reliable way was to use RegExp.
Method #1: The Classic — replace() + RegExp (still relevant)
This is the "grandfather" method that works in all browsers, including Internet Explorer (yes, it's still alive somewhere). The secret is the g flag (global).
let text = "The cat ate the mouse. The cat is happy.";let newText = text.replace(/cat/g, "dog");console.log(newText); // "The dog ate the mouse. The dog is happy."Pros:
- Works everywhere (IE11, old Node.js versions)
- Powerful: you can use capture groups, the
iflag (case-insensitivity) - Very fast
Cons:
- Need to escape special characters (dot, asterisk, etc.)
- Code is less readable for beginners
- Not suitable if the replacement string is dynamic (comes from user input)
If you need to replace a dynamic string, use the RegExp constructor:
function escapeRegExp(string) { return string.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');}
let userInput = "hello.world";let text = "hello.world, how are you? hello.world!";let safePattern = escapeRegExp(userInput);let result = text.replace(new RegExp(safePattern, 'g'), 'goodbye.all');console.log(result);// "goodbye.all, how are you? goodbye.all!"The escapeRegExp function is a mandatory ritual if you are working with user input. Otherwise, a dot in the string will be interpreted as "any character".
Method #2: The Modern Standard — replaceAll() (ES2021+)
Starting from ES2021 (ECMAScript 2021), we have String.prototype.replaceAll(). This is the answer to the question "how do I replace all occurrences of a string in JavaScript" that was awaited for years.
let text = "I love JavaScript, JavaScript is awesome!";let newText = text.replaceAll("JavaScript", "Python");console.log(newText); \