The easiest would be to use a regular expression with g flag to replace all instances:

str.replace(/foo/g, "bar")

This will replace all occurrences of foo with bar in the string str. If you just have a string, you can convert it to a RegExp object like this:

var pattern = "foobar",
    re = new RegExp(pattern, "g");
Answer from Gumbo on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › replaceAll
String.prototype.replaceAll() - JavaScript - MDN Web Docs
While it is also possible to use replace() with a global regex dynamically constructed with RegExp() to replace all instances of a string, this can have unintended consequences if the string contains special characters that have meaning in regular expressions (which might happen if the replacement string comes from user input).
Discussions

Best way to find and replace all instances if a string in an array of strings?
Currently I am doing this by iterating through each of the arrays and checking each value for the replaceable values and replacing the matches. That's pretty much what you'd need to do. map() is the method used for accomplishing this var arr = ["826", "7161", "", "", "x", "927", "hah", "hg7)", "x"] var arrWithClosed = arr.map(str => !str || str === "x" ? "Closed" : str) console.log(arrWithClosed) // 826, 7161, Closed, Closed, Closed, 927, hah, hg7), Closed More on reddit.com
🌐 r/learnjavascript
8
5
January 6, 2023
Replace all instances of variable name with other
after sources are indexed, right-click symbol, then select rename More on reddit.com
🌐 r/Xcode
9
2
January 21, 2022
🌐
freeCodeCamp
freecodecamp.org › news › javascript-replaceall-replace-all-instances-of-a-string-in-js
JavaScript replaceAll() – Replace All Instances of a String in JS
July 28, 2022 - The replaceAll() method is part of JavaScript's standard library. When you use it, you replace all instances of a string.
🌐
CoreUI
coreui.io › blog › how-to-replace-all-occurrences-of-a-string-in-javascript
How to replace all occurrences of a string in JavaScript? · CoreUI
August 31, 2024 - In this example, the global regular expression /hello/g matches all instances of ‘hello’, ensuring that every occurrence is replaced with ‘hi’. This is one of the most common ways to replace multiple occurrences of a substring in JavaScript. When using a regular expression, special characters like . or * have special meanings. If your search string contains these characters, you’ll need to escape them using a backslash (\):
🌐
Sentry
sentry.io › sentry answers › javascript › how do i replace all occurrences of a string in javascript?
How do I Replace all Occurrences of a String in JavaScript?
Replace all occurrences of a substring in JavaScript using replaceAll with strings or regex, the replace method with a global flag, or split and join
🌐
Crio
crio.do › blog › how-to-replace-all-occurrences-of-a-string-in-javascript-2024-criodo
How Do I Replace All Occurrences of a String in JavaScript?
December 9, 2024 - var string = "Test abc test test ... all occurrences, JavaScript offers several solutions: The replaceAll() method replaces all instances of a substring in the string....
🌐
DigitalOcean
digitalocean.com › community › tutorials › replace-all-instances-of-a-string-in-javascript
How To Replace All Instances of a String in JavaScript | DigitalOcean
October 28, 2020 - Normally JavaScript’s String replace() function only replaces the first instance it finds in a string: ... const myMessage = 'this is the sentence to end all sentences'; const newMessage = myMessage.replace('sentence', 'message'); console.log(newMessage); // this is the message to end all ...
Find elsewhere
🌐
John Kavanagh
johnkavanagh.co.uk › home › articles › replace all instances of a string in javascript
Replace All Instances of a String in JavaScript | John Kavanagh
May 4, 2026 - Here, we take a quick look into using JavaScript replace() method, both with and without regular expressions. By default, the replace() method will only replace the first instance it comes across within a string.
🌐
Netalith
netalith.com › blogs › javascript-tutorial › how-to-replace-all-instances-of-a-string-in-javascript
How To Replace All Instances of a String in JavaScript - Netalith
February 28, 2026 - Replace all occurrences of a string in javascript using regex /g or replaceAll. Examples: escaping special characters and case-insensitive matching. Netalith
🌐
Dmitri Pavlutin
dmitripavlutin.com › replace-all-string-occurrences-javascript
3 Ways To Replace All String Occurrences in JavaScript
January 27, 2023 - You can replace all occurrences of a string using split and join approach, replace() with a regular expression and the new replaceAll() string method.
🌐
Delft Stack
delftstack.com › home › howto › javascript › replace all instances of a string in javascript
How to Replace All Instances of a String in JavaScript | Delft Stack
February 2, 2024 - It should be fairly obvious that the general solution involves using split() for the string you want to search for and join() for the string you want to replace it with.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-string-replaceall-method
JavaScript String replaceAll() Method - GeeksforGeeks
July 23, 2025 - The function GFG() defines a regular expression /coffee/ig to match all occurrences of "coffee" case-insensitively in the string "Lets, have coffee today!". It then replaces all occurrences with "tea" and logs the modified string "Lets, have tea today!" to the console. ... function GFG() { const regexp = /coffee/ig; let string = "Lets, have coffee today!"; newString = string.replaceAll(regexp, "tea"); console.log(newString); } GFG(); ... Lets, have tea today! We have a complete list of Javascript string methods, to check those please go through the Javascript String Complete Reference article.
🌐
Go Make Things
gomakethings.com › how-to-replace-all-instances-of-a-string-with-another-with-vanilla-js
How to replace all instances of a string with another with vanilla JS | Go Make Things
September 7, 2020 - The String.replaceAll() method works just like the String.replace() method, but it replaces all instances of a string instead of the first matching one. // Awkwardly worded, but roll with it var wizards = 'Of all the wizards in Lord of the Rings, ...
🌐
JavaScript Tutorial
javascripttutorial.net › home › how to replace all occurrences of a substring in a string
How To Replace All Occurrences of a Substring in a String in JavaScript
September 9, 2020 - The String type provides you with the replace() and replaceAll() methods that allow you to replace all occurrences of a substring in a string and return the new version of the string.
🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › string-replace
Replace All Instances of a String in JavaScript
June 3, 2019 - The `setTimeout()` function in JavaScript sets a function to run later in a non-blocking way. Here's what you need to know. ... The `trim` option in Mongoose schema definitions makes Mongoose automatically call `String.prototype.trim()` on string fields.
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-replace-all-occurrences-of-a-string-in-javascript.php
How to Replace All Occurrences of a String in JavaScript
<script> /* Define function for ... functin to find and replace specified term with replacement string */ function replaceAll(str, term, replacement) { return str.replace(new RegExp(escapeRegExp(term), 'g'), replacement); } ...
🌐
HackerNoon
hackernoon.com › 5-simple-ways-to-replace-all-string-occurrences-in-javascript
5 Simple Ways to Replace All String Occurrences in JavaScript | HackerNoon
March 7, 2023 - JavaScript has multiple methods for replacing strings, one of which is the String.prototype.replace() method. This method has two compulsory parameters—the substring which needs to be replaced, and what it needs to be replaced with—and two ...
🌐
Flavio Copes
flaviocopes.com › home › javascript › how to replace all occurrences of a string in javascript
How to replace all occurrences of a string in JavaScript
July 2, 2018 - To replace all occurrences of a string in JavaScript you have 3 options: call replace() with a regular expression that has the g flag, chain split() and join(), or use the newer replaceAll() method.
🌐
Vultr Docs
docs.vultr.com › javascript › examples › replace-all-instances-of-a-character-in-a-string
JavaScript Program to Replace all Instances of a Character in a String | Vultr Docs
December 17, 2024 - ... let initialString = "Hello ... with '0', resulting in "Hell0 World". Use a regular expression with the global (g) flag to replace all occurrences of a specific character....
🌐
Zipy
zipy.ai › blog › how-do-i-replace-all-occurrences-of-a-string-in-javascript
how do i replace all occurrences of a string in javascript
April 12, 2024 - The replaceAll() method is a new addition to JavaScript introduced in ES2021 (ECMAScript 2021). It provides a more straightforward way to replace all occurrences of a substring within a string, without the need for regular expressions.