Javascript 如何使正则表达式不区分大小写?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5744582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make regex case-insensitive?
提问by Nir
I have this javascript code but when i send this: asd.JPG the regex fails to me..
我有这个 javascript 代码,但是当我发送这个代码时:asd.JPG 正则表达式对我来说失败了..
if (data.match(/([^\/\]+)\.(jpg|jpeg|gif|png|tiff|tif)$/i))
return { filename: RegExp., ext: RegExp. };
else
return { filename: "invalid file type", ext: null };
So I want that the regex looks at the extension as case-insensitive. I tried this but it fails:
所以我希望正则表达式将扩展视为不区分大小写。我试过这个,但它失败了:
data.match(/([^\/\]+)\.(?i)(jpg|jpeg|gif|png|tiff|tif)$/i)
Any Ideas?
有任何想法吗?
回答by T.J. Crowder
The i
flag you have on the end (/.../i
) should be doing it.
最后的i
标志 ( /.../i
) 应该这样做。
(CW because let's face it, one shouldn't earn rep for this sort of thing... :-) )
(CW 因为让我们面对现实吧,这种事情不应该赢得代表...... :-) )