views:

289

answers:

3

I have the following code which I use to match fancybox possible elements:

$('a.grouped_elements').each(function(){
    var elem = $(this);
    // Convert everything to lower case to match smart
    if(elem.attr('href').toLowerCase().match('/gif|jpg|jpeg|png/') != null) {
        elem.fancybox();
    }
});

It works great with JPGs but it isn't matching PNGs for some reason. Anyone see a bug with the code? Thanks

+2  A: 

I guess the fact that it'll match anywhere in the string (it would match "http://www.giftshop.com/" for instance) could be considered a bug. I'd use

/\.(gif|jpe?g|png)$/i
Matti Virkkunen
+7  A: 
Anurag
A: 

You are passing a string to the match() function rather than a regular expression. In JavaScript, strings are delimited with single quotes, and regular expressions are delimited with forward slashes. If you use both, you have a string, not a regex.

Jan Goyvaerts