1

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~!

Dalí
  • 102
  • 1
  • 9

3 Answers3

2

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
}
1

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.


Community
  • 1
  • 1
Scott
  • 3,736
  • 2
  • 26
  • 44
  • 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 01:41
1

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

Terry
  • 63,248
  • 15
  • 96
  • 118