简体   繁体   English

使用数组元素进行搜索并使用javascript替换所有内容?

[英]Using array elements to do a search and replace all using javascript?

Say, I have a string 说,我有一个字符串

'I am abc1, my age is abc2 and I live in abc3, abc1'

and array which looks like 和看起来像的数组

['abc1/xyx1', 'abc2/xyx2', 'abc3/xyx3']

I'm trying to do a search and replace all on the string using each element of the array in below fashion 我正在尝试搜索并使用以下方式使用数组的每个元素替换字符串上的所有内容

replace abc1 by xyz1, abc2 by xyz2 etc..

ie after the script runs the output string will be 即脚本运行后输出字符串将是

I am xyz1, my age is xyz2 and I live in xyz3, xyz1

This is what I tried thus far 这是我到目前为止所尝试的

var myString = 'I am abc1, my age is abc2 and I live in abc3, abc1';
var myArray = ['abc1/xyx1', 'abc2/xyx2', 'abc3/xyx3'];
for (var i=0; i<=myArray.length; i++){
var a1 = myArray[i];
var xs = a1.split("/");
var new1=xs[0];
var new2=xs[1];
var replaced = myString.replace(/new1/g, new2);
}
document.write(replaced);

But it is not working. 但它没有用。 Can anybody help? 有人可以帮忙吗?

Basically you loop one too much, take i < myArray.length and you need the value of the variable as regular expression. 基本上你循环太多,拿i < myArray.length你需要变量的值作为正则表达式。

You could use a constructor and build a new regular expression object. 您可以使用构造函数并构建新的正则表达式对象。

At last, you need to replace the same string and assign to the same string, otherwise you hae replaced, but you get only the last replacement back. 最后,您需要替换相同的字符串并分配给相同的字符串,否则您已被替换,但您只能获得最后一次替换。

 var myString = 'I am abc1, my age is abc2 and I live in abc3, abc1', myArray = ['abc1/xyx1', 'abc2/xyx2', 'abc3/xyx3'], i, xs, new1, new2; for (i = 0; i < myArray.length; i++) { xs = myArray[i].split("/"); new1 = xs[0]; new2 = xs[1]; myString = myString.replace(new RegExp(new1, 'g'), new2); } console.log(myString); 

Try this approach. 试试这种方法。

For first a split the keys/values and get a new array ( keyValue ) which holds the old and new words. 首先拆分keys/values并获得一个新数组( keyValue ),它保存旧词和新词。 Then I iterate over the keyValue and replace the text by it's values. 然后我遍历keyValue并用它的值替换text

 var text = 'I am abc1, my age is abc2 and I live in abc3, abc1'; var words = ['abc1/xyx1', 'abc2/xyx2', 'abc3/xyx3']; var keyValue = words.map(item => { var arr = item.split('/'); return [arr[0], arr[1]]; }); keyValue.forEach(item => { text = text.replace(new RegExp(item[0], 'g'), item[1]); }); console.log(text); 

You can iterate over the words array usign String.prototype.forEach() and in every loop split the element w with String.prototype.split() to create an array variable a with the two indexed elements that you need to create a regular expression and call String.prototype.replace() : 您可以遍历words array usign String.prototype.forEach()并在每个循环中使用String.prototype.split()拆分元素w以创建一个数组变量a ,其中包含创建正则表达式所需的两个索引元素并调用String.prototype.replace()

 var text = 'I am abc1, my age is abc2 and I live in abc3, abc1'; var words = ['abc1/xyx1', 'abc2/xyx2', 'abc3/xyx3']; words.forEach(function (w) { var a = w.split('/'); text = text.replace(new RegExp(a[0], 'g'), a[1]); }); console.log(text); 

A functional approach: 功能方法:

 var text = 'I am abc1, my age is abc2 and I live in abc3, abc1'; var words = ['abc1/xyx1', 'abc2/xyx2', 'abc3/xyx3']; var result = words.reduce((text, word) => { var [ oldWord, newWord ] = word.split('/'); return text.replace(new RegExp(oldWord, 'g'), newWord); }, text); console.log(result); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM