簡體   English   中英

如何找到字符串中的某些單詞?

[英]How to find certain words in string?

我有一個字符串數組,每個字符串都包含一個關鍵字和一個數字(數字值不同,只有關鍵字是常數),我想知道如何獲取該單詞以及數字...請參閱下面的例子:

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]

如果您想使用正則表達式方法,

該示例可能是您想要的:

var found = [],
    result;   

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

“找到的”數組中,您應該獲得一個項目列表,每個項目都包含以下數據:

  1. 原始關鍵字匹配項(帶有數字的關鍵字字符串)
  2. 關鍵字字符串部分(無數字)
  3. 僅數字(從關鍵字中提取)

輸出示例:

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

希望能有所幫助。

此代碼可以完成工作

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

非常簡單的方法:

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

...更通用,更實用的方法...

 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