I have a string prototype whose code is given below:
String.prototype.replaceAll = function(str1, str2, ignore) {
return this.replace(
new RegExp(
str1.replace(/([\/\,\!\\\^\$\{\}\[\]\(\)\.\*\+\?\|\<\>\-\&])/g,"\\$&"),(ignore?"gi":"g")),(typeof(str2)=="string")?str2.replace(/\$/g,"$$$$"
):str2
)};
Usage:
var a = "I am Javascript";
console.log(
a.replaceAll("am", "love")
); // => I love Javascript
But when it comes to multiple exchange of characters or words, I have to run the prototype multiple times to achieve it. But I have thought of something like this:
var a = "I am Java";
console.log(
a.replaceAll(["am" , "Java"], ["love", "Javascript"])
); // => I love Javascript
So can you help me to achieve it? Or there is any other alternative?