如何使用 JavaScript 使用正则表达式验证图像文件扩展名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29732756/
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 validate image file extension with regular expression using JavaScript
提问by jobin
How to validate image file extension (jpg,png,gif,bmp) with JavaScript regular expression?
如何使用 JavaScript 正则表达式验证图像文件扩展名(jpg、png、gif、bmp)?
回答by jsxt
Accordingly your issue the regular expression is quite simple.
因此,您的问题正则表达式非常简单。
/\.(jpe?g|png|gif|bmp)$/i
Do you really sure that nothing else will be used? For example, JPEG format allows both .jpg and .jpeg extensions. That's why I put e?
pattern in the regular expression.
你真的确定不会使用其他任何东西吗?例如,JPEG 格式允许 .jpg 和 .jpeg 扩展名。这就是我e?
在正则表达式中加入模式的原因。
Example of validation could be as follows:
验证示例如下:
var filename = "/site/images/test.png";
if ( /\.(jpe?g|png|gif|bmp)$/i.test(filename) ) {
. . .
.
.