Regex JavaScript image file extension

JavascriptRegexImage

Javascript Problem Overview


I need a regular expression to validate image file extensions in javascript. Do you have one ?

Javascript Solutions


Solution 1 - Javascript

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 parentheses () so as to disambiguate between the slash (/) operator and RegExp object. See JSLint for more details.

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionprcaenView Question on Stackoverflow
Solution 1 - Javascriptg13nView Answer on Stackoverflow