Javascript 如何检查文本中的任何单词是否在数组中?

Javascript 如何检查文本中的任何单词是否在数组中?,javascript,jquery,Javascript,Jquery,给出一个完整的文本,如: var nation = "Piazza delle Medaglie d'Oro 40121 Bologna Italy" 以及一个给定的数组,如: ["Afghanistan", "Italy", "Albania", "United Arab Emirates"] 我们如何检查整个文本中的单词意大利是否在数组中 下面是我尝试过的,但是我得到了False,而意大利出现在数组中 var countries = []; $("#usp-custom-3 o

给出一个完整的文本,如:

var nation = "Piazza delle Medaglie d'Oro
40121 Bologna
Italy"
以及一个给定的数组,如:

 ["Afghanistan", "Italy", "Albania", "United Arab Emirates"]
我们如何检查整个文本中的单词意大利是否在
数组中

下面是我尝试过的,但是我得到了
False
,而意大利出现在
数组中

  var countries = [];
  $("#usp-custom-3 option").each(function() {
    var single = $(this).text();
    countries.push(single);
    var foundPresent = countries.includes("Piazza delle Medaglie d'Oro 40121 Bologna Italy");
    console.log(foundPresent); 
  });

如果在每次推送到数组时都进行检查,则更简单,只需检查推送的元素:

const text = " I like Italy";
const nations=[];

function insert(single){
 if( text.includes(single) /*may format single, e.g. .trim() etc*/){
   alert("Nation in text!");
 }
 nations.push(single);
}


如果仍希望每次都检查整个数组,则嵌套迭代可能会执行以下操作:

let countries = ["Afghanistan", "Italy", "Albania", "United Arab Emirates"];
const text = " I like Italy";

let countriesInText = countries.filter( word => text.includes( word ) );
//["Italy"]

如果您只关心是否,可以使用.some()而不是.filter()

var nation=“意大利博洛尼亚Medaglie广场40121”;
意大利(“意大利”,国家);
函数searchStringInArray(str,strArray){

对于(var j=0;j由于需要在数组中搜索包含单词的字符串,所以最好的选择是使用正则表达式并使用
string.match(regex)
来获得匹配的单词

var nation=`Piazza delle Medaglie d'Oro
40121博洛尼亚
意大利`;
//var nation=“意大利博洛尼亚梅达格利广场40121”;
var国家=[“阿富汗”、“意大利”、“阿尔巴尼亚”、“阿拉伯联合酋长国”];
var regex=new RegExp(countries.join(“|”)i);

console.log(nation.match(regex))
此脚本将把var nation中的每个单词与您提供的数组的所有元素进行比较。我希望这将解决您的问题

<script>
    var nation = "Piazza delle Medaglie d'Oro 40121 Bologna Italy";
    test = nation.split(" ");
    array = ["Afghanistan", "Italy", "Albania", "United Arab Emirates"];
    test.forEach(function (element) {
        array.forEach(function (array_to_compare) {
            if (element == array_to_compare)
                alert("We found that word " + element + " matches in array");
        });
    }, this);
    each()

</script>

var nation=“意大利博洛尼亚梅达格利广场40121”;
测试=国家分割(“”);
数组=[“阿富汗”、“意大利”、“阿尔巴尼亚”、“阿拉伯联合酋长国”];
测试forEach(功能(元素){
forEach(函数(数组与数组比较){
if(元素==数组\u到\u比较)
警报(“我们发现单词“+元素+”在数组中匹配”);
});
},这个);
各()
$(函数(){
试一试{
var nation=“意大利博洛尼亚梅达格利广场40121”;
var I=[“阿富汗”、“意大利”、“阿尔巴尼亚”、“阿拉伯联合酋长国”]
对于(var指数=0;指数=0){
console.log(I[index].toString());
}
}
}
捕捉(错误){
控制台日志(err);
}
});

试试这个。

你假设数组中的一个单词不能在文本中的一个单词中。你不能跳过标记化。这意味着没有理由反过来检查。实际上我更喜欢这个答案,更清晰,可读性更强。(+1)@Jonasw有一些类似于开发人员的结果。不是快速、干净、可读的代码。我在工作中遇到了这场战斗哈哈-所以我最终为“结果”而不是后者编写代码。@denys Seguret 1)取决于文本中的定义2)因为不需要基于OPs EDIT检查整个数组,所以这一个对我来说更有意义,而且因为我不擅长正则表达式,我需要它更快、更可读,所以我在这里接受这一个。它还使用了
includes
,这是我根据问题中的代码所做的工作是的,好的,发生了,简单的家伙。我的问题现在被编辑了。哈哈-这真是个笑话,一个可靠的问题-带有代码和尝试。还有很好的答案。一些人在权力之旅。@tez只是投票重新开放…愿大多数人获胜;@rob.m这是由于初始的不完整性。它将在某个时候重新开放。投了票。:-)哦,这是s哦,实际上,我从来没有想过使用正则表达式,我的头脑无法控制它们,但看起来很好here@rob.m您显示的文本有新行。?不是吗?@rob.m很高兴我能提供帮助。请记住,一个构造良好的问题总是会吸引更好的解决方案,因为问题陈述定义良好。显示努力/代码也会在这方面帮助用户正在改进/良好实践。因此,如果可能的话,分享它们。@Rajesh会做的,通常只需快速点击发送按钮即可,谢谢man@rob.m您可以执行
var matches=nation.match(regex);if(matches!=null&&matches.length>0){…}
或仅
匹配!=null
也足够了,因为如果找不到匹配项,它将返回
null
<script>
    var nation = "Piazza delle Medaglie d'Oro 40121 Bologna Italy";
    test = nation.split(" ");
    array = ["Afghanistan", "Italy", "Albania", "United Arab Emirates"];
    test.forEach(function (element) {
        array.forEach(function (array_to_compare) {
            if (element == array_to_compare)
                alert("We found that word " + element + " matches in array");
        });
    }, this);
    each()

</script>
$(function () {

    try {
        var nation = "Piazza delle Medaglie d'Oro 40121 Bologna Italy";

        var I = ["Afghanistan", "Italy", "Albania", "United Arab Emirates"]

        for (var index = 0; index < I.length; index++) {
            if (nation.toString().toUpperCase().indexOf(I[index].toString().toUpperCase()) >= 0) {
                console.log(I[index].toString());
            }
        }
    }
    catch (err) {
        console.log(err);
    }
});