javascript - RegExp to check if file is image - Stack Overflow

Is there a way to know if the file extension is an image?i got this.imagepngAlready try with var image

Is there a way to know if the file extension is an image?

i got this.

image/png

Already try with

var imageReg = /\.(gif|jpg|jpeg|tiff|png)$/i;
string = "image/png"
imageReg.test(string)

But this return false;

Is there a way to know if the file extension is an image?

i got this.

image/png

Already try with

var imageReg = /\.(gif|jpg|jpeg|tiff|png)$/i;
string = "image/png"
imageReg.test(string)

But this return false;

Share asked Jun 23, 2015 at 4:46 carloss medranoocarloss medranoo 1,4893 gold badges15 silver badges21 bronze badges 2
  • 2 Well, the path image/png has no extension, it's as simple as that. – Ja͢ck Commented Jun 23, 2015 at 4:57
  • as simple as i didnt know and thats why i ask that – carloss medranoo Commented Jun 23, 2015 at 5:03
Add a ment  | 

1 Answer 1

Reset to default 13

Put dot and / inside a character class so that it would match .png or /png strings.

var imageReg = /[\/.](gif|jpg|jpeg|tiff|png)$/i;

Your regex would return true if there is a dot before png but here there exists a forward slash, so it fails.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743675163a4488435.html

相关推荐

  • javascript - RegExp to check if file is image - Stack Overflow

    Is there a way to know if the file extension is an image?i got this.imagepngAlready try with var image

    6天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信