Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

Problem

I know how to find out if a string contains a substring like this where you are looking for one word:

var s = "define foo";
alert(s.indexOf("define") > -1);

But how could I check for multiple different words/substrings using an array?

Example not working code that makes sense in my mind but doesn't work:

query = "Define what is grape juice and how to drink it?"
var terms = ["define", "what is", "how to"];
alert(query.indexOf(terms) > -1);

Thanks~!

share|improve this question

3 Answers 3

up vote 0 down vote accepted

You can use $.each() in jQuery to iterate through the terms array, and check each of them individually against the string. In the code below, I create a new JSON object called matchedTerms that will log the term and its index in the string. See demo here: http://jsfiddle.net/teddyrised/ktuuoprp/1/

var query = "Define what is grape juice and how to drink it?",
    terms = ["define", "what is", "how to"],
    matchedTerms = [];

$.each(terms, function(i,v) {
    var match = query.indexOf(v);
    matchedTerms.push({
        'term': v,
        'index': match
    });
});

Even better: you can build a conditional statement in there so that the matchedTerms will only produce a simple array. See demo here: http://jsfiddle.net/teddyrised/ktuuoprp/2/

var query = "Define what is grape juice and how to drink it?",
    terms = ["define", "what is", "how to"],
    matchedTerms = [];

$.each(terms, function(i,v) {
    var match = query.indexOf(v);
    if(match > -1) matchedTerms.push(v);
});

console.log(matchedTerms);

p/s: If you want to perform case-insensitive matches, it helps to convert the query into lowercase, i.e. newQuery = query.toLowerCase(query);

share|improve this answer

You want to see if an array includes a string. There are a few ways to do it. This is answered well here.

Here are 2 options, copied from there:

Option 1:

$.indexOf is effectively a wrapper for Array.prototype.indexOf in browsers that support it (almost all of them these days), while providing a shim in those that don't. It is essentially equivalent to adding a shim to Array.prototype, which is a more idiomatic/JSish way of doing things. MDN provides such code. These days I would take this option, rather than using the jQuery wrapper.

Option 2:

jQuery offers $.inArray:

var found = $.inArray('specialword', categories) > -1;

Note that inArray returns the index of the element found, so 0 indicates the element is the first in the array. -1 indicates the element was not found.

Example.


share|improve this answer
    
I think you've cited a reverse example — OP is asking for matching words in an array against a string to see if each element in the array exists or not. –  Terry Dec 17 '14 at 1:41

Try this out:

var phrase = 'texttexttexttexttexttexttext';
var terms = ['word1', 'word2', 'word3'];
function check(string) {
    var match = false;
    for(var i=0;i<terms.length && !match;i++) {
        if(string.indexOf(terms[i]) > -1) {
            match = true;
        }
    }
    return match;
}
//example
if(check(phrase)) {
    //iftrue
} else {
    //iffalse
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.