tomster
tomster

Reputation: 90

jQuery.grep usage for finding keywords and phrases

Need help with finding keywords and phrases from a textarea using jQuery.grep so that it is not an exact match but contains certain keyword(s) and/or phrase(s) in an array.

Right now this only works if the textarea is the exact word or phrase matching the array:

function isSpam(array, name){
    return $.grep(array, function(i) {
        return i == name;
    }).length > 0;
}

Link: http://jsfiddle.net/2ejC5/

But does not work if there is a match and some other text. Not sure how the syntax should be to make it work matching any keywords and phrases from the array.

Also, is there an alternate way to do this with indexOf?

Upvotes: 0

Views: 430

Answers (1)

Barmar
Barmar

Reputation: 780698

function isSpam(array, name){
    return $.grep(array, function(i) {
        return name.indexOf(i) >= 0;
    }).length > 0;
}

FIDDLE

Upvotes: 1

Related Questions