When we use replace() in any string, most of the times it is not easy to predict how many occurrences we will have to replace. There can be many approaches to do it. Let's have a look at all o them and proceed to the best one.
- Use Fail-Safe
Using more replace() in chain than expected occurrences, this will not harm in anyway but it is not a good practice because no. of occurrence is not predictable.
- var StrToRepalce ="Scala is a Functional Programming Language. Scala is easy to learn.";
-
- StrToRepalce = StrToRepalce.replace('Scala', 'F#')
- .replace('Scala', 'F#')
- .replace('Scala', 'F#')
- .replace('Scala', 'F#');
- console.log(StrToRepalce);
- split() join() combination
This approach will work for any number of occurrences, but it is more performance costly.
- var StrToRepalce ="Scala is a Functional Programming Language. Scala is easy to learn.";
- StrToRepalce = StrToRepalce.split("Scala").join("F#");
- console.log(StrToRepalce);
- Using Regular Expression
Very short and simple. Regex will work for any no. of occurrences, but it is error prone for special characters.
- var StrToRepalce ="Scala is a Functional Programming Language. Scala is easy to learn.";
-
- StrToRepalce = StrToRepalce.replace(/Scala/g, 'F#');
-
- console.log(StrToRepalce);
- RegEx with Special Character handling
Same as previous approach, but it is capable of handling special characters too.
- var StrToRepalce ="Scala is a Functional Programming Language. Scala is easy to learn.";
- StrToRepalce = StrToRepalce.replace(new RegExp("Scala".replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), 'g'), "F#");
- console.log(StrToRepalce);
- Adding replaceAll() to string prototype to use globally
The above approaches are all good but it is better to place this function to string prototype so can be run from anywhere across the application.
- String.prototype.replaceAll = function (find, replace) {
- var str = this;
- return str.replace(new RegExp(find.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), 'g'), replace);
- };
- var StrToRepalce ="Scala is a Functional Programming Language. Scala is easy to learn.";
- StrToRepalce = StrToRepalce.replaceAll("Scala", "F#");
- console.log(StrToRepalce);
Code - https://gist.github.com/AshishVishwakarma/386dfe2a0e1ba22ce29a456d93258e77
Live Preview - http://codepen.io/AshishVishwakarma/pen/BKaONd