简体   繁体   中英

Javascript: return true if ALL strings inside an array are present in another string

I have to check if a string inside a variable has all the strings inside an array on it. Very simple I thing, but I cant get it to work! I have tried a lot of things but are to complex and not really working. I'm looking to some suggestion.

Something like:

var myArray = ["cat","dog","bird"];
var myString = "There is a cat looking the bird and a dog looking the cat";
var myString2 = "There is a cat and a dog looking one each other";

myArray and myString must be true and true and myArray and myString2 must result false

I was working with something like this:

var selector = "dir.class#id";
var code = '<div id="id" class="class"></div>';
var tokens = selector.split(/[\.,#]+/);

for (var i = tokens.length - 1; i >= 0; i--) {
    var counter = [];
    if (code.indexOf(tokens[0]) > -1 ) {
        counter.concat(true);
    }
}

Thanks!

You can do something like bellow

var arePresentInText = function (text,words) {
    return words.every(function(word){
        return text.indexOf(word) > -1;
    });
}

console.log(arePresentInText(myString,myArray)); //prints true
console.log(arePresentInText(myString2,myArray)); //prints false

This should work:

 var myArray = ["cat","dog","bird", "cat"];
 var myString = "There is a cat looking the bird and a dog looking the cat";
 var myString2 = "There is a cat and a dog looking one each other";

 function checkValue(arr, str){
    var cnt = 0;
    for(i in arr){
        var val = arr[i];
        if(str.indexOf(val) > -1) cnt++;
    }

    return (arr.length == cnt) ? true : false;
 }

 console.log(checkValue(myArray, myString));
function checkContainStr(arr, str){
for(i in arr){
    if(str.indexOf(arr[i]) == -1)
     return false;
}
 return true;
} 

Try this function out:

function arrayElementsInString(ary, str){
  for(var i=0,l=ary.length; i<l; i++){
    if(str.indexOf(ary[i]) === -1){
      return false;
    }
  }
  return true;
}

If you want to match strings at the end of words but not necessarily at the start, you'll need to use either a regular expression or other processing.

Checking with indexOf matches the character sequence anywhere in the word, not at the end. Consider the following which matches the strings either at the end or as the whole word:

  var myArray = ["cat","dog","bird"]; var myString = "There is a cat looking the bird and a dog looking the cat"; var myString2 = "There is a cat and a dog looking one each other"; var myString3 = "There is a classcat and a iddog looking at bird"; var myString4 = "There is a catclass and a dog looking at bird"; document.write(myArray.every(function(word) { return (new RegExp('\\\\w*' + word + '\\\\b').test(myString)); }) + '<br>'); document.write(myArray.every(function(word) { return (new RegExp('\\\\w*' + word + '\\\\b').test(myString2)); }) + '<br>'); document.write(myArray.every(function(word) { return (new RegExp('\\\\w*' + word + '\\\\b').test(myString3)); }) + '<br>'); document.write(myArray.every(function(word) { return (new RegExp('\\\\w*' + word + '\\\\b').test(myString4)); })); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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