简体   繁体   English

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

[英]How to find certain words in string?

I have an array of strings and each string contains a key word plus a number (the number value is different, only the key word is constant) and I want to know how can I get the word and also the number...see the example below: 我有一个字符串数组,每个字符串都包含一个关键字和一个数字(数字值不同,只有关键字是常数),我想知道如何获取该单词以及数字...请参阅下面的例子:

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'"]

And the response should be like this: 响应应该是这样的:

var response = ["data4","data110","action13","type23","action1023","type48"];

This is what I've managed to do: 这是我设法做到的:

  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]);
      }
    }
  }

But it only returns me the exact key words, without numbers.. 但这只会返回确切的关键词,没有数字。

response = [data, data, action, action, type, type]

If you would like to take the regex approach, 如果您想使用正则表达式方法,

this example might be what you were looking for: 该示例可能是您想要的:

var found = [],
    result;   

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

in the "found" array you should get a list of items, each with the following data: “找到的”数组中,您应该获得一个项目列表,每个项目都包含以下数据:

  1. the original keyword match (the keyword string with the number) 原始关键字匹配项(带有数字的关键字字符串)
  2. the keyword string part (without the number) 关键字字符串部分(无数字)
  3. the number only (extracted from the keyword) 仅数字(从关键字中提取)

An example output: 输出示例:

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

Hope it helps a bit. 希望能有所帮助。

This code does the job 此代码可以完成工作

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);

Very straightforward approach: 非常简单的方法:

 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)) 

... a more generic and functional approach ... ...更通用,更实用的方法...

 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); 

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

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