Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegExp to check if file is 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;

like image 614
carloss medranoo Avatar asked Sep 03 '25 15:09

carloss medranoo


1 Answers

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.

like image 103
Avinash Raj Avatar answered Sep 05 '25 05:09

Avinash Raj