简体   繁体   English

Javascript在一个字符串中获取多个子字符串

[英]Javascript Get Multiple Substrings Within One String

how to get multiple occurences of words in a string. 如何获得字符串中单词的多次出现。 I've tried multiple functions but with no success. 我尝试了多种功能,但没有成功。 I know I can get back the first true value using some() method as below. 我知道我可以使用some()方法获得第一个真实值,如下所示。

       var keyword_array = ["Trap","Samples","WAV","MIDI","Loops"];
        function validateContentKeywords(content,keyword){
                keyword.some(function(currentValue,index){

                     console.log(currentValue + " ");

                     return content.indexOf(currentValue) >= 0;         
                });          
        }
     // Outputs --> Trap Samples
       if(validateContentKeywords("Beat Loops WAV Trap Samples Dog Cat MIDI",keyword_array)){
                console.log("Matches");     
        }
    // What I Want is --> Trap,Samples,MIDI,Loops

The above function only outputs 2 occurences and I want it to output all of the matching values at the same time such as --> Trap,Samples,MIDI,Loops. 上面的函数只输出2个事件,我希望它同时输出所有匹配值,例如-> Trap,Samples,MIDI,Loops。 Is there a way to get multiple occurences of words in a string at the same time? 有没有一种方法可以同时获取字符串中单词的多次出现?

UPDATED:: The solution that helped me out is below 更新::帮助我解决的解决方案如下

           function Matches(value){
                return "Beat Loops WAV Trap Samples Dog Cat MIDI".indexOf(value) !== -1;
           }
           var keyword_array = ["Trap","Samples","WAV","MIDI","Loops"].filter(Matches);
            document.write(keyword_array);

您似乎正在寻找可以返回匹配元素数组的filter Array方法 ,而不是返回是否匹配的布尔值。

 var keyword_array = ["Trap", "Samples", "WAV", "MIDI", "Loops"]; function validateContentKeywords(content, keyword) { var words = content.split(' '); //split the given string for (var i = 0; i < words.length; i++) { if (keyword.indexOf(words[i]) > -1) { //check if actually iterated word from string is in the provided keyword array document.write(words[i] + " "); //if it is, write it to the document }; } } validateContentKeywords("Beat Loops WAV Trap Samples Dog Cat MIDI", keyword_array); 

The easiest way to do it would be this: 最简单的方法是:

keyword_array.filter(keyword => searchString.includes(keyword));

You can learn more about filter here . 您可以在此处了解有关filter更多信息。 I highly recommend learning about how to use map , reduce and filter . 我强烈建议您学习如何使用mapreducefilter They are your best friends. 他们是你最好的朋友。

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

相关问题 Javascript - 如何在一个条件下检查字符串是否包含多个子字符串 - Javascript - How to check if a string contains multiple substrings in one condition 如何在JavaScript中多个子串中的一个子串上拆分字符串? - How to split a string on the first occurrence of one of multiple substrings in JavaScript? JavaScript字符串中多个子字符串的计数 - Count of multiple substrings in JavaScript string 替换字符串中的多个子字符串-Javascript - replacing multiple substrings in a string - Javascript 获取 JavaScript 中字符串的所有子字符串 - Get all substrings of a string in JavaScript 在较大的字符串中搜索多个子字符串 - Search for multiple substrings within a larger string Javascript - 如何检查一个字符串是否包含多个子字符串 - Javascript - How to check if a string contains multiple substrings jquery/javascript 检查多个子字符串的字符串 - jquery/javascript check string for multiple substrings JavaScript - 从优化的角度来看,应该如何用不同的字符串替换字符串中的多个子字符串? - JavaScript - From an optimization standpoint how should one replace multiple substrings in a string with different strings? 如何获得多个子字符串的数组,该数组由Javascript中的相同字符串/符号/标签除 - How to get an array of multiple substrings divided by the same string/symbol/tag in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM