1

I would like the following if statement to alert 'yes' if 'featured' is in array, regardless of whether it is uppercase or lowercase.

I'm also not very familiar with Regular Expressions, so perhaps my regular expression is wrong..

var myArray = ["featured", "foo", "bar"];

if( $.inArray(/featured/i, myArray) > -1 ){
    alert('yes!');
}

Also, here is a jsfiddle of the above

5 Answers 5

4

I don't think $.inArray supports regular expressions, but you could make your own function that does.

$.inArrayRegEx = function(regex, array, start) {
    if (!array) return -1;
    start = start || 0;
    for (var i = start; i < array.length; i++) {
        if (regex.test(array[i])) {
            return i;
        }
    }
    return -1;
};

Working demo: http://jsfiddle.net/jfriend00/H4Y9A/


You could even override the existing inArray to make it "optionally" support regular expressions while still retaining its regular functionality:

(function() {
    var originalInArray = $.inArray;
    $.inArray = function(regex, array, start) {
        if (!array) return -1;
        start = start || 0;
        if (Object.prototype.toString.call(regex) === "[object RegExp]") {
            for (var i = start; i < array.length; i++) {
                if (regex.test(array[i])) {
                    return i;
                }
            }
            return -1;
        } else {
            return originalInArray.apply(this, arguments);
        }
    };
})();

Working demo: http://jsfiddle.net/jfriend00/3sK7U/

Note: the one thing you would lose with this override is the ability to find a regular expression object itself in an array.

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

1 Comment

Added an override of the existing $.inArray that would add support for regular expressions. Also, added support for the optional 3rd parameter to $.inArray().
3

An array is not a string, but you can make it a string, by using toString

if(/featured/.test(myArray.toString())){
    alert('yes!');
}

WORKING DEMO

4 Comments

This wont work, you cant check for commas this way (as .toString adds some)
Yes it does add commas, but he doesn't care for commas. If you check the fiddle, you will see that it does work.
It does work for that particular example, but we don't know how far OP is going with this. Also this might be counterproductive for people who come here in search for a solution for their own problem.
Well, the scope of the question doesn't address how far he is going with this. We can be responsible for all edge cases.
1

No, I don't think so.

This is how I would to it, using ($.each and match()):

var myArray = ["featured", "foo", "bar"];


$.each(myArray,function(index,value){
    if(value.match(/featured/i) !== null){
        alert("Yes!");
        return false;
    }
});

on JSFIDDLE: http://jsfiddle.net/QGtZU/

1 Comment

That may cause multiple alerts, op wants to check if there is a case.
0

Version using Javascript exec and forEach. It achieves the same but does not use inArray.

var myArray = ["featured", "foo", "bar"];

var exp = /featured/i;

myArray.forEach( function(value){

   if( exp.exec(value) ){
    alert("yes");
   }

} );

Fiddle

Comments

0

You can use $.grep to filter the array and then check the length:

 if ( $.grep( myArray, function (e, i ){ return /featured/i.test(e); }).length > 0 )
    alert( "YES!" );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.