javascript字符串替换为数组值的所有出现

时间:2016-11-07 08:59:15

标签: javascript arrays node.js string replace

我想替换所有出现的字符串,但问题是我有一个删除单词和数组的数组。删除单词值。

例如:

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]

4 个答案:

答案 0 :(得分:1)

如果您更喜欢修复方法,可以使用RegExp构造函数构建带变量的动态regexp,并确保只匹配包含带有单词边界的模式的整个单词。不要忘记转义文字模式,并在循环之前声明finalAns并使用string var内容进行初始化。



var string = "this is string to replace. this string should replace using javascript";
var replaceArray = ["this","is","string","should","javascript"];
var replaceArrayValue = ["There","are","strings","have to","node.js"];
var finalAns = string;
for (var i = replaceArray.length - 1; i >= 0; i--) {
    finalAns = finalAns.replace(RegExp("\\b" + replaceArray[i].replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') + "\\b", "g"), replaceArrayValue[i]);
}
console.log(finalAns);




请注意,使用单个静态正则表达式的Roman's approach似乎对当前任务最有效(不需要每次都动态生成regexp)。

答案 1 :(得分:1)

使用替换功能使用以下方法:



var string = "this is string to replace. this string should replace using javascript",
    replaced = string.replace(/\b\w+\b/g, function ($m) {
        var search = ["this","is","string","should","javascript"],
            replacement = ["There","are","strings","have to","node.js"],
            key = search.indexOf($m);

        return (key !== -1)? replacement[key] : $m;
    });

console.log(replaced);




来自" 高性能JavaScript " (尼古拉斯扎卡斯):

  

" ...关于超出范围的变量:存储任何常用的变量   局部变量中超出范围的变量,然后访问本地变量   变量直接。"

答案 2 :(得分:1)

没有常规Exp

只能在循环中设置变量 string =finalAns;。因为每次循环设置旧字符串然后发生问题,

var string = "this is string to replace. this string should replace using javascript";

var replaceArray = ["this","is","string","should","javascript"];

var replaceArrayValue = ["There","are","strings","have to","node.js"];
alert(replaceArray.length);
for (var i = replaceArray.length - 1 ; i >= 0; i--) {

    var finalAns = string.replace(replaceArray[i],replaceArrayValue[i]);
    string =finalAns;
}
alert(finalAns);

Snippet Exmaple



var string = "this is string to replace. this string should replace 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]);
    string =finalAns;
}
alert(finalAns);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

我使用声明式方法找到了一个解决方案:

const string = 'boo and Boo2'
const dictionary = { boo: 'faa', boo2: 'faa2' }
return string.split(' ')
  .map(word => dictionary[word.toLowerCase()] ?? word)
  .join(' ')
// 'faa and faa2'