RegExp to check if file is image Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of RegExp to check if file is image without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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;

Answer

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.

We are here to answer your question about RegExp to check if file is image - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji