0

I am using jQuery to check if a string is contained in any of the strings in an array like so -

var arrayText = ["the","cat","sat"];
arrayText.indexOf($('#textBox1').val()) > -1)

However this only appears to match exact cases, however is it possible to change it so that it matches part matches, e.g. "he" is present in "the" therefore the result would be greater than -1?

2

2 Answers 2

0

I think this would work for you

var arrayText = ["the", "cat", "sat"];
if ($.grep(arrayText, function (e, i) {
    return /at/i.test(e);
}).length > 0) {
    console.log('matched')
}

DEMO

Hope this helps

Sign up to request clarification or add additional context in comments.

Comments

0

If you want to find whether an array has an element which is a sub-string of another string then ;

var arrayText = ["the","cat","sat"];

Array.prototype.containsSubString = function( text ){
    for ( var i in this )
    {
        if ( this[i].toString().indexOf( text ) != -1 )
            return i;
    }
    return -1;
}

if( arrayText.containsSubString( 'he' ) !== -1) 
{
    alert("Found in arrayText !");
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.