I need a regular expression to validate image file extensions in javascript. Do you have one ?
Asked
Active
Viewed 8.1k times
52
-
http://en.wikipedia.org/wiki/Image_file_formats and http://www.regular-expressions.info/tutorial.html – Felix Kling May 06 '12 at 18:50
-
http://stackoverflow.com/questions/169625/regex-to-check-if-valid-url-that-ends-in-jpg-png-or-gif. – benastan May 06 '12 at 18:55
1 Answers
164
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.

Niels Rask
- 3
- 5

g13n
- 3,218
- 2
- 24
- 19
-
28
-
7
-
1
-
1How would this look like when you also want to include capitalised extensions? And thank you for your answer – Woww Apr 06 '21 at 09:12