如何在字符串中找到某些单词?

时间:2016-09-13 09:48:02

标签: javascript arrays regex string search

我有一个字符串数组,每个字符串包含一个关键字加一个数字(数字值不同,只有关键字是常量)我想知道如何得到这个词和数字.. 。看下面的例子:

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]

4 个答案:

答案 0 :(得分:2)

如果您想采用正则表达式方法,

这个例子可能就是你要找的东西:

var found = [],
    result;   

for(var i in stringArray){
   result = stringArray[i].match(/(data|action|value|type)(\d+)/);
   found.push(result);
}

“found”数组中,您应该获得一个项目列表,每个项目都包含以下数据:

  1. 原始关键字匹配(带有数字的关键字字符串)
  2. 关键字字符串部分(不带数字)
  3. 仅限数字(从关键字中提取)
  4. 示例输出:

    [["data4", "data", "4"], ["data110", "data", "110"], ["action1023", "action", "1023"], ["type23", "type", "23"], ["action13", "action", "13"], ["type48", "type", "48"]]
    

    希望它有所帮助。

答案 1 :(得分:1)

此代码完成工作

var keyWords = ["data","action","value","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 = [];

for(var i = 0; i < stringArray.length; ++i){
    for(var j = 0; j < keyWords.length; ++j){
        if((index = stringArray[i].indexOf(keyWords[j])) !== -1){
            splittedStr = stringArray[i].substring(index,         stringArray[i].length).split(' ', 1);
            response.push(splittedStr[0]);
        }
    }
}
console.log(response);

答案 2 :(得分:0)

非常简单的方法:

&#13;
&#13;
var keyWords = ["data","action","value","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'"]

function f(keyWords, stringArray){
	let output = []
	
	stringArray.forEach((sentence) => {
		var words = sentence.split(' ')
	
		words.forEach((word) => {
			keyWords.forEach((keyword) => {			
				if(word.indexOf(keyword) > -1 ) {
					output.push(word)
				}				
			})
		})
	})
		
	return output
}

console.log(f(keyWords, stringArray))
&#13;
&#13;
&#13;

答案 3 :(得分:0)

......一种更通用和功能性的方法......

var
    keyList       = ["data", "action", "value", "type"],

    sentenceList  = [
        "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'"
    ],

    wordList = sentenceList.reduce(function collectSpecificWords (collector, sentence) {
        var
            matchList = sentence.split(/\s+/).filter(function (word) {

                return collector.regXWord.test(word);
            });

        collector.wordList = collector.wordList.concat(matchList);

        return collector;

    }, {

      //regXWord: RegExp("^("+ keyList.join("|") + ")[0-9]+$"),
        regXWord: RegExp("^("+ keyList.join("|") + ")[0-9]*$"),
        wordList: []

    }).wordList;


console.log("wordList : ", wordList);