简体   繁体   English

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

[英]How to check if any word inside a text is in array?

Given a whole text like: 给出一个完整的文字,如:

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

And a given array like: 和给定的数组如:

 ["Afghanistan", "Italy", "Albania", "United Arab Emirates"]

How can we check that the word Italy within that whole text is in the array ? 我们怎样才能检查整个文本中的意大利语是否在array

Following this SO answer this is what I tried, but I get False while instead Italy is present within the array 在这个SO答案之后,这就是我尝试过的,但是我得到了False ,而在array存在意大利

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

JsFiddle here JsFiddle在这里

If you check whenever you push to an array, its even much simpler, just check the pushed element: 如果你检查每次推送到一个数组,它甚至更简单,只需检查推送元素:

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

Run


If you still want to check the whole array everytime, a nested iteration may does it: 如果您仍希望每次检查整个数组,嵌套迭代可能会这样做:

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

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

Run

Performance compared to Rajeshs answer 性能与Rajeshs的回答相比

If you just care if or if not, may use .some() instead of .filter() . 如果您只关心是否,可以使用.some()而不是.filter()

Since you need to search a string with words in an array, best option is to use a regex and use string.match(regex) to get the matched words. 由于您需要在数组中搜索包含单词的字符串,因此最好选择使用正则表达式并使用string.match(regex)来获取匹配的单词。

 var nation = `Piazza delle Medaglie d'Oro 40121 Bologna Italy`; //var nation = "Piazza delle Medaglie d'Oro 40121 Bologna Italy"; var countries = ["Afghanistan", "Italy", "Albania", "United Arab Emirates"]; var regex = new RegExp(countries.join("|"), "i"); console.log(nation.match(regex)) 

var nation = "Piazza delle Medaglie d'Oro 40121 Bologna Italy";
searchStringInArray("Italy", nation);
function searchStringInArray (str, strArray) {
    for (var j=0; j<strArray.length; j++) {
        if (strArray[j].match(str)) return j;
    }
    return -1;
}

this script will compare every word from var nation with all elements of array that you provided. 此脚本将比较var nation中的每个单词与您提供的所有数组元素。 I hope that this will solve your issue 我希望这能解决你的问题

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

try this one. 试试这个。

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

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