Answer by MMMahdy-PAPION for How do I replace all occurrences of a string in...
Of course in 2021 the right answer is:String.prototype.replaceAll()console.log('Change this and this for me'.replaceAll('this','that') // Normal case);console.log('aaaaaa'.replaceAll('aa','a') //...
View ArticleAnswer by Alecx for How do I replace all occurrences of a string in JavaScript?
I have read this question and answers, but I didn't find an appropriate solution for me. Although the answers are quite useful, I decided to create my own solution from scratch. The problems about this...
View ArticleAnswer by Ran Turner for How do I replace all occurrences of a string in...
String.prototype.replaceAll - ECMAScript 2021The new String.prototype.replaceAll() method returns a new string with all matches of a pattern replaced by a replacement. The pattern can be either a...
View ArticleAnswer by pedro casas for How do I replace all occurrences of a string in...
Repeat until you have replaced them all:const regex = /^>.*/im;while (regex.test(cadena)) { cadena = cadena.replace(regex, '*');}
View ArticleAnswer by Oliver M Grech for How do I replace all occurrences of a string in...
After several trials and a lot of fails, I found that the below function seems to be the best all-rounder when it comes to browser compatibility and ease of use. This is the only working solution for...
View ArticleAnswer by francis for How do I replace all occurrences of a string in...
With the regular expression i flag for case insensitiveconsole.log('App started'.replace(/a/g, '')) // Result: "App strted"console.log('App started'.replace(/a/gi, '')) // Result: "pp strted"
View ArticleAnswer by Force Bolt for How do I replace all occurrences of a string in...
// Try this wayconst str = "Test abc test test abc test test test abc test test abc";const result = str.split('abc').join('');console.log(result);
View ArticleAnswer by Satish Chandra Gupta for How do I replace all occurrences of a...
JavaScript provides a direct way to replace a part of a string with another string and there are some tricks also by which you can do this.To replace all the occurrences you can use replace() or...
View ArticleAnswer by Chungmin Park for How do I replace all occurrences of a string in...
There is a way to use the new replaceAll() method.But you need to use a cutting-edge browser or a JavaScript run time environment.You can check the browser compatibility in here.
View ArticleAnswer by Drewry Pope for How do I replace all occurrences of a string in...
This solution combines some previous answers and conforms somewhat better to the proposed August 2020 standard solution. This solution is still viable for me in September 2020, as String.replaceAll is...
View ArticleAnswer by Nisharg Shah for How do I replace all occurrences of a string in...
In August 2020No more regular expression stuffconst str = "Test abc test test abc test test test abc test test abc";const modifiedStr = str.replaceAll('abc',...
View ArticleAnswer by Mohit Yadav for How do I replace all occurrences of a string in...
I would suggest adding a global method for the string class by appending it to the prototype chain.String.prototype.replaceAll = function(fromReplace, toReplace, {ignoreCasing} = {}) { return...
View ArticleAnswer by Asakkour Soufiane for How do I replace all occurrences of a string...
Use split and join:var str = "Test abc test test abc test test test abc test test abc";var replaced_str = str.split('abc').join('');console.log(replaced_str);
View ArticleAnswer by Iftikhar Hussain for How do I replace all occurrences of a string...
Here's a very simple solution.You can assign a new method to a String objectString.prototype.replaceAll = function(search, replace){ return this.replace(new RegExp(search, 'g'), replace)}var str =...
View ArticleAnswer by Matěj Štágl for How do I replace all occurrences of a string in...
Starting from v85, Chrome now supports String.prototype.replaceAll natively. Note this outperform all other proposed solutions and should be used once majorly supported.Feature...
View ArticleAnswer by Shavais for How do I replace all occurrences of a string in...
I added the function below to this performance test page in the "library" section:https://jsben.ch/LFfWAfunction _replace(t, s, r){ var i = t.indexOf(s); if (i == -1) return t; return t.slice(0, i) + r...
View ArticleAnswer by e102 for How do I replace all occurrences of a string in JavaScript?
I know this isn’t the best way to do this, but you can try this:var annoyingString = "Test abc test test abc test test test abc test test abc";while (annoyingString.includes("abc")) { annoyingString =...
View ArticleAnswer by Lova Chittumuri for How do I replace all occurrences of a string in...
We can use the replace method in JavaScript:var result = yourString.replace('regexPattern', "replaceString");var str = "Test abc test test abc test test test abc test test abc";var expectedString =...
View ArticleAnswer by Kamil Kiełczewski for How do I replace all occurrences of a string...
PerformanceToday 27.12.2019 I perform tests on macOS v10.13.6 (High Sierra) for the chosen solutions.ConclusionsThe str.replace(/abc/g, ''); (C) is a good cross-browser fast solution for all...
View ArticleAnswer by Ace for How do I replace all occurrences of a string in JavaScript?
In November 2019, a new feature is added to the JavaScript, string.prototype.replaceAll().Currently it's only supported with Babel, but maybe in the future it can be implemented in all the browsers....
View Article