I'm trying to make a function that finds the string that contains all words from an array.
I have tried this:
function multiSearchOr(text, searchWords){
var searchExp = new RegExp(searchWords.join("|"),"gi");
return (searchExp.test(text))?"Found!":"Not found!";
}
alert(multiSearchOr("Hello my name sam", ["Hello", "is"]))
But this only alert "Found" when one of the words have been found.
I need it to alert me when all the words are in the string.
An example:
var sentence = "I love cake"
var words = ["I", "cake"];
I want the application to alert me when it finds all of the words from the array in the string sentence. Not when it only found one of the words.