How do I replace all occurrences of a string?
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...
View ArticleAnswer by scronide for How do I replace all occurrences of a string?
Match against a global regular expression:anotherString = someString.replace(/cat/g, 'dog');
View ArticleAnswer by user21926 for How do I replace all occurrences of a string?
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...
View ArticleAnswer by SolutionYogi for How do I replace all occurrences of a string?
str = str.replace(/abc/g, '');Or try the replaceAll method, as recommended in this answer:str = str.replaceAll('abc', '');or:var search = 'abc';str = str.replaceAll(search, '');EDIT: Clarification...
View ArticleAnswer by Donnie DeBoer for How do I replace all occurrences of a string?
Use a regular expression:str.replace(/abc/g, '');
View ArticleAnswer by Matthew Crumley for How do I replace all occurrences of a string?
In the latest versions of most popular browsers, you can use replaceAllas shown here:let result = "1 abc 2 abc 3".replaceAll("abc", "xyz");// `result` is "1 xyz 2 xyz 3"But check Can I use or another...
View ArticleAnswer by Victor for How do I replace all occurrences of a string?
My implementation, very self explanatoryfunction replaceAll(string, token, newtoken) { if(token!=newtoken) while(string.indexOf(token) > -1) { string = string.replace(token, newtoken); } return...
View ArticleAnswer by Elias Van Ootegem for How do I replace all occurrences of a string?
Use word boundaries (\b)'a cat is not a caterpillar'.replace(/\bcat\b/gi,'dog');//"a dog is not a caterpillar"This is a simple regex that avoids replacing parts of words in most cases. However, a dash...
View ArticleAnswer by Owen for How do I replace all occurrences of a string?
I like this method (it looks a little cleaner):text = text.replace(new RegExp("cat","g"), "dog");
View ArticleAnswer by Raseela for How do I replace all occurrences of a string?
Loop it until number occurrences comes to 0, like this:function replaceAll(find, replace, str) { while (str.indexOf(find) > -1) { str = str.replace(find, replace); } return str;}
View ArticleAnswer by rakslice for How do I replace all occurrences of a string?
If what you want to find is already in a string, and you don't have a regex escaper handy, you can use join/split:function replaceMulti(haystack, needle, replacement){ return...
View ArticleAnswer by Cory Gross for How do I replace all occurrences of a string?
For the sake of completeness, I got to thinking about which method I should use to do this. There are basically two ways to do this as suggested by the other answers on this page.Note: In general,...
View ArticleAnswer by Antonio Mirarchi for How do I replace all occurrences of a string?
Try this:String.prototype.replaceAll = function (sfind, sreplace) { var str = this; while (str.indexOf(sfind) > -1) { str = str.replace(sfind, sreplace); } return str;};
View ArticleAnswer by Cole Lawrence for How do I replace all occurrences of a string?
This is the fastest version that doesn't use regular expressions.Revised jsperfreplaceAll = function(string, omit, place, prevstring) { if (prevstring && string === prevstring) return string;...
View ArticleAnswer by zdennis for How do I replace all occurrences of a string?
while (str.indexOf('abc') !== -1){ str = str.replace('abc', '');}
View ArticleAnswer by Guy for How do I replace all occurrences of a string?
If using a library is an option for you then you will get the benefits of the testing and community support that goes with a library function. For example, the string.js library has a replaceAll()...
View ArticleAnswer by SamGoody for How do I replace all occurrences of a string?
If you are trying to ensure that the string you are looking for won't exist even after the replacement, you need to use a loop.For example:var str = 'test aabcbc';str = str.replace(/abc/g, '');When...
View ArticleAnswer by Tim Rivoli for How do I replace all occurrences of a string?
function replaceAll(str, find, replace) { var i = str.indexOf(find); if (i > -1){ str = str.replace(find, replace); i = i + replace.length; var st2 = str.substring(i); if(st2.indexOf(find) > -1){...
View ArticleAnswer by tk_ for How do I replace all occurrences of a string?
You can simply use below method/** * Replace all the occerencess of $find by $replace in $originalString * @param {originalString} input - Raw string. * @param {find} input - Target key word or regex...
View ArticleAnswer by Sandeep Gantait for How do I replace all occurrences of a string?
The following function works for me:String.prototype.replaceAllOccurence = function(str1, str2, ignore){ return this.replace(new...
View Article