【发布时间】:2016-09-13 09:48:01
【问题描述】:
我有一个字符串数组,每个字符串都包含一个关键字和一个数字(数值不同,只有关键字是常数),我想知道如何获取单词和数字.. .看下面的例子:
var keyWords = ["data","action","type"];
var stringArray = ["set data4 to custom value '1'",
"set data110 to custom value '2'",
"overwrite value of action1023 with new value",
"set type23 to custom value '10'",
"overwrite value of action13 with text",
"set type48 to custom value '7'"]
响应应该是这样的:
var response = ["data4","data110","action13","type23","action1023","type48"];
这是我设法做到的:
var response = [];
for(var j=0; j<keyWords.length; j++) {
for(var i=0; i<stringArray.length; i++) {
if(stringArray[i].indexOf(keyWords[j]) !== -1) {
response.push(keyWords[j]);
}
}
}
但它只返回确切的关键词,没有数字..
response = [data, data, action, action, type, type]
【问题讨论】:
-
我只是设法从数组中获取关键字,但没有数字...
-
@Typo 我会尝试使用正则表达式来实现解决方案,谢谢!
标签: javascript arrays regex string search