我希望替换字符串中的所有出现,但问题在于我有一个要删除的单词数组和对应的删除值。
例如:
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]);
}
我希望能得到类似于以下内容的翻译:
There are strings to replace. There strings have to be replaced using node.js
我在这里找到了一些解决方案,其中最好的解决方案是这里。但我不能使用/string/g
中的字符串,我必须使用replaceArray[i]
。
var regex = new RegExp(replaceArray[i], 'g')
- Rajesh