[英]regex replace a string match from array element with different array element
[英]How to replace string in an element in an array with a string from an element in another array?
嗨,我的目标是使用Javascript将array1(2x2)中与arrayChoose(3x1)中的单词匹配的所有单词替换为arrayReplacewith(3x1)中的单词。 我尝试了包括映射和替换在内的几种方法,但是我正在绕圈而行,不确定如何进行。
编辑:只是为了添加一些上下文,我正在从csv文件加载所有数组。 选择和替换数组的长度也不一定相同。
edit2:面临的问题:我见过的所有示例都针对矢量,而不是矩阵。 其次,任何在所有元素中替换字符串的解决方案都只能替换字符串的第一个实例,而我无法实现全局替换。
任何帮助将非常感激。 谢谢。
var array1 = [["today is the day","or maybe it's tomorrow"],["who knows really","it's just where we are"]];
var arrayChoose = ["today", "tomorrow", "just"];
var arrayReplacewith = ["monday", "tuesday", "not"];
解决方案感谢所有提供答案的人。 我利用了@ mohamed-ibrahim提供的那个。 请参阅以下解决方案...(从csv文件加载的数组,请参见此处以获取完整示例)
var arrayIncorrect = [ [ 'Trump has ordered', 'flight attendants to stop' ],
[ 'shorter people to ', 'restrict tourism.' ],
[ 'Now all they ', 'need is a wall.' ] ];
var dataChoose = [ [ 'Trump' ],
[ 'stop' ],
[ 'people' ],
[ 'restrict' ],
[ 'wall' ] ];
var dataReplace = [ [ 'Kim Jong-un' ],
[ 'wear' ],
[ 'skirts' ],
[ 'boost' ],
[ 'airplane' ] ];
arrayCorrect=
arrayIncorrect.map(function(array1){
return array1.map(function(ele){
dataChoose.forEach(function(choose){
ele = ele.replace(new RegExp(choose, 'g'), dataReplace[dataChoose.indexOf(choose)]) ;
})
return ele;
})
});
console.log(arrayCorrect)
如有任何问题,请告诉我。
请尝试以下操作:
var array1 = [["today is the day","or maybe it's tomorrow"],["who knows really","it's just where we are"]];
var arrayChoose = ["today", "tomorrow", "just"];
var arrayReplacewith = ["monday", "tuesday", "not"];
array1 =
array1.map(function(array2){
return array2.map(function(ele){
arrayChoose.forEach(function(choose){
ele = ele.replace(new RegExp(choose, 'g'), arrayReplacewith[arrayChoose.indexOf(choose)]) ;
})
return ele;
})
});
两个简单的map
可以解决问题,如果select数组的条目多于replace数组,则可能会引发错误,因此您需要确保不是这种情况。
var array1 = ["today is the day", "or maybe it's tomorrow", "who knows really", "it's just where we are"]; var arrayChoose = ["today", "tomorrow", "just"]; var arrayReplacewith = ["monday", "tuesday", "not"]; array1 = array1.map(v => { let s = v.split(" ").map(st => { let index = arrayChoose.indexOf(st); return index > -1 ? arrayReplacewith[index] : st; }); return s.join(" "); }); console.log(array1);
一种使用带有ES6 arrow functions
.map
和.indexOf
arrow functions
:
replaceArr = (arrOriginal, arrWordsToMatch, arrWordsToReplace) => {
if (arrWordsToMatch.length != arrWordsToReplace.length) throw "arrWordsToMatch and arrWordsToReplace must have exact length"
return arrOriginal.map( (phrase) => {
return phrase.split(" ").map( (word) => {
let foundIndex = arrWordsToMatch.indexOf(word)
return (foundIndex > -1) ? arrWordsToReplace[foundIndex] : word
}).join(" ")
})
}
let newArray = replaceArr(array1, arrayChoose, arrayReplacewith);
console.log(newArray);
/*
[ 'monday is the day',
'or maybe it\'s tuesday',
'who knows really',
'it\'s not where we are' ]
*/
注意:由于arrWordsToMatch
和arrWordsToReplace
必须具有相同的长度,因此,如果大小不同,则会引发错误。
您可以使用正则表达式和对象作为替换字符串。
var array = [["today is the day", "or maybe it's tomorrow"], ["who knows really", "it's just where we are"]], object = { today: 'monday', tomorrow: 'tuesday', just: 'not' }, result = array.map(function (a) { return a.map(function(b) { return b.replace(RegExp(Object.keys(object).join('|'), 'g'), function (s) { return object[s]; }); }); }); console.log(result);
ES6
var array = [["today is the day", "or maybe it's tomorrow"], ["who knows really", "it's just where we are"]], object = { today: 'monday', tomorrow: 'tuesday', just: 'not' }, result = array.map(a => a.map(b => b.replace(RegExp(Object.keys(object).join('|'), 'g'), s => object[s]))); console.log(result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.