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 Top answer 1 of 14
1321
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");
2 of 14
165
Try this replaceAll: http://dumpsite.com/forum/index.php?topic=4.msg8#msg8
String.prototype.replaceAll = function(str1, str2, ignore)
{
return this.replace(new RegExp(str1.replace(/([\/\,\!\\\^\$\{\}\[\]\(\)\.\*\+\?\|\<\>\-\&])/g,"\\$&"),(ignore?"gi":"g")),(typeof(str2)=="string")?str2.replace(/\
$$$"):str2);
}
It is very fast, and it will work for ALL these conditions that many others fail on:
"x".replaceAll("x", "xyz");
// xyz
"x".replaceAll("", "xyz");
// xyzxxyz
"aA".replaceAll("a", "b", true);
// bb
"Hello???".replaceAll("?", "!");
// Hello!!!
Let me know if you can break it, or you have something better, but make sure it can pass these 4 tests.
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).
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
Replace all instances of variable name with other
after sources are indexed, right-click symbol, then select rename More on reddit.com
How To Replace All Occurrences Of A String In JavaScript?
02:31
How to replace all occurrences of a string in JavaScript? - YouTube
10:54
JavaScript Problem: Replace All Occurrences of a String - YouTube
How do I replace all occurrences of a string in JavaScript?
02:20
JS tips: replace all instances in a string WITHOUT regex! (FIX ...
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 (\):
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 ...
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.
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.