I want to replace all occurrences of a string but the issue is that I have an array of remove words & remove words value.
for example :
var string = "this is a string to replace. This string should be replaced using javascript";
var replaceArray = ["this","is","string","should","javascript"];
var replaceArrayValue = ["There","are","strings","have to","node.js"];
for (var i = replaceArray.length - 1; i >= 0; i--) {
var finalAns = string.replace(replaceArray[i],replaceArrayValue[i]);
}
I am expecting something like
There are strings to replace. There strings have to be replaced using node.js
I found some solutions in which I got the best solution here.
but I can't use the string in /string/g
. I have to use replaceArray[i]