正则表达式 JavaScript 图像文件扩展名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10473185/
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
Regex JavaScript image file extension
提问by prcaen
I need a regular expression to validate image file extensions in javascript. Do you have one ?
我需要一个正则表达式来验证 javascript 中的图像文件扩展名。你是否有一个 ?
回答by g13n
Could you explain the purpose?
你能解释一下目的吗?
Anyway here's one assuming you support few image types.
无论如何,这里假设您支持几种图像类型。
(/\.(gif|jpe?g|tiff|png|webp|bmp)$/i).test(filename)
I have put the whole regular expression within parens () so as to disambiguate between the slash (/) operator and RegExp object. See JSLint for more details.
我已将整个正则表达式放在 parens () 中,以便消除斜杠 (/) 运算符和 RegExp 对象之间的歧义。有关更多详细信息,请参阅 JSLint。
Here's the raw regex as well.
这也是原始的正则表达式。
/\.(gif|jpe?g|tiff|png|webp|bmp)$/i
This Regex also assumes that you're including the dot before the extension. Remove the \.
if you're not.
此正则表达式还假定您在扩展名之前包含点。\.
如果不是,请删除。