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

Javascript 如何在字符串中查找某些单词?,javascript,arrays,regex,string,search,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 valu

我有一个字符串数组,每个字符串包含一个关键字和一个数字(数字值不同,只有关键字是常量),我想知道如何获取单词和数字…请参见下面的示例:

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

非常简单的方法:

var关键字=[“数据”、“操作”、“值”、“类型”];
var stringArray=[“将数据4设置为自定义值'1'”,
“将data110设置为自定义值'2'”,
“用新值覆盖action1023的值”,
将type23设置为自定义值“10”,
“用文本覆盖action13的值”,
“将type48设置为自定义值'7'”
函数f(关键字,字符串数组){
让输出=[]
stringArray.forEach((句子)=>{
变量词=句子。拆分(“”)
words.forEach((word)=>{
关键字.forEach((关键字)=>{
if(word.indexOf(关键字)>-1){
输出推送(word)
}				
})
})
})
返回输出
}

log(f(关键字,字符串数组))
非常简单的方法:

var关键字=[“数据”、“操作”、“值”、“类型”];
var stringArray=[“将数据4设置为自定义值'1'”,
“将data110设置为自定义值'2'”,
“用新值覆盖action1023的值”,
将type23设置为自定义值“10”,
“用文本覆盖action13的值”,
“将type48设置为自定义值'7'”
函数f(关键字,字符串数组){
让输出=[]
stringArray.forEach((句子)=>{
变量词=句子。拆分(“”)
words.forEach((word)=>{
关键字.forEach((关键字)=>{
if(word.indexOf(关键字)>-1){
输出推送(word)
}				
})
})
})
返回输出
}
log(f(关键字,stringArray))
此代码完成此任务

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关键字=[“数据”、“操作”、“值”、“类型”];
var stringArray=[“将数据4设置为自定义值'1'”,
“将data110设置为自定义值'2'”,
“用新值覆盖action1023的值”,
将type23设置为自定义值“10”,
“用文本覆盖action13的值”,
“将type48设置为自定义值'7'”
var响应=[];
对于(变量i=0;i
此代码执行此任务

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关键字=[“数据”、“操作”、“值”、“类型”];
var stringArray=[“将数据4设置为自定义值'1'”,
“将data110设置为自定义值'2'”,
“用新值覆盖action1023的值”,
将type23设置为自定义值“10”,
“用文本覆盖action13的值”,
“将type48设置为自定义值'7'”
var响应=[];
对于(变量i=0;i
如果您想采用正则表达式方法

此示例可能就是您正在寻找的:

var found = [],
    result;   

for(var i in stringArray){
   result = stringArray[i].match(/(data|action|value|type)(\d+)/);
   found.push(result);
}
在“find”数组中,您应该得到一个项目列表,每个项目都包含以下数据:

  • 原始关键字匹配(带数字的关键字字符串)
  • 关键字字符串部分(不带数字)
  • 仅数字(从关键字中提取)
  • 输出示例:

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

    希望能有所帮助。

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

    此示例可能就是您正在寻找的:

    var found = [],
        result;   
    
    for(var i in stringArray){
       result = stringArray[i].match(/(data|action|value|type)(\d+)/);
       found.push(result);
    }
    
    在“find”数组中,您应该得到一个项目列表,每个项目都包含以下数据:

  • 原始关键字匹配(带数字的关键字字符串)
  • 关键字字符串部分(不带数字)
  • 仅数字(从关键字中提取)
  • 输出示例:

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

    希望能有点帮助。

    。。。一种更通用、更实用的方法

    var
    keyList=[“数据”、“操作”、“值”、“类型”],
    句子列表=[
    将data4设置为自定义值“1”,
    “将data110设置为自定义值'2'”,
    “用新值覆盖action1023的值”,
    将type23设置为自定义值“10”,
    “用文本覆盖action13的值”,
    “将type48设置为自定义值“7”
    ],
    wordList=sentenceList.reduce(函数collectSpecificWords(收集器,句子){
    变量
    匹配列表=句子。拆分(/\s+/)。过滤器(函数(单词){
    返回收集器.regXWord.test(word);
    });
    collector.wordList=collector.wordList.concat(匹配列表);
    回程收集器;
    }, {
    //regXWord:RegExp(“^(“+keyList.join”(“|”)+”[0-9]+$”,
    regXWord:RegExp(“^(“+keyList.join”(“|”)+”[0-9]*$”,
    词表:[]
    }).字表;
    
    log(“单词列表:”,单词列表)。。。一种更通用、更实用的方法

    var
    keyList=[“数据”、“操作”、“值”、“类型”],
    句子列表=[
    将data4设置为自定义值“1”,
    “将data110设置为自定义值'2'”,
    “用新值覆盖action1023的值”,
    将type23设置为自定义值“10”,
    “用文本覆盖action13的值”,
    “将type48设置为自定义值“7”
    ],
    wordList=sentenceList.reduce(函数collectSpecificWords(收集器,句子){
    变量
    匹配列表=句子。拆分(/\s+/)。过滤器(函数(单词){
    返回收集器.regXWord.test(word);
    });
    科尔