JS / Jquery,匹配找不到PNG =匹配('/ gif | jpg | jpeg | png /') [英] JS/Jquery, Match not finding the PNG = match('/gif|jpg|jpeg|png/')

查看:140
本文介绍了JS / Jquery,匹配找不到PNG =匹配('/ gif | jpg | jpeg | png /')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码用于匹配fancybox可能的元素:

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

它适用于JPG但由于某些原因它不匹配PNG。有人看到代码的错误吗?
谢谢

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

推荐答案

一些事情。

匹配接受 RegExp 的对象,而不是字符串。它可能在某些浏览器中有效,但绝对不是标准的。

Match accepts an object of RegExp, not a string. It may work in some browsers, but is definitely not standard.

"gif".match('/gif|png|jpg/'); // null​​​​​​​​​​​​​​​​​​​​​​​​​​​​

没有字符串

"gif".match(/gif|png|jpg/); // ["gif"]

此外,您还需要在文件名末尾检查这些内容,而不是字符串中的任何位置。

Also, you would want to check these at the end of a filename, instead of anywhere in the string.

"isthisagif.nope".match(/(gif|png|jpg|jpeg)/); // ["gif", "gif"]

仅在带有$ suffix的字符串末尾搜索

Only searching at the end of string with $ suffix

"isthisagif.nope".match(/(gif|png|jpg|jpeg)$/); // null

无需将href设为小写,只需进行不区分大小写的搜索 / I

No need to make href lowercase, just do a case insensitive search /i.

在图片扩展名前面找一个点作为附加支票。

Look for a dot before the image extension as an additional check.

以及一些测试。我不知道你是如何使用 .match <的字符串参数得到任何结果的/ code>。你在用什么浏览器?

And some tests. I don't know how you got any results back with using a string argument to .match. What browser are you on?

这篇关于JS / Jquery,匹配找不到PNG =匹配('/ gif | jpg | jpeg | png /')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆