我想替換所有出現的字符串,但問題是我有一個刪除單詞的數組&刪除單詞的值。javascript string替換所有發生的數組值
例如:
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
我發現了一些解決方案中,我得到了最好的解決方案here。 但我不能使用/string/g
中的字符串。我必須使用replaceArray[i]
可以。 'var regex = new RegExp(replaceArray [i],'g')' – Rajesh