【发布时间】:2017-03-20 13:31:36
【问题描述】:
我想替换所有出现的字符串,但问题是我有一个删除单词数组和删除单词值。
例如:
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')
标签: javascript arrays node.js string replace