I need a regular expression to validate image file extensions in javascript. Do you have one ?
-
en.wikipedia.org/wiki/Image_file_formats and regular-expressions.info/tutorial.htmlFelix Kling– Felix Kling2012-05-06 18:50:40 +00:00Commented May 6, 2012 at 18:50
-
stackoverflow.com/questions/169625/….benastan– benastan2012-05-06 18:55:37 +00:00Commented May 6, 2012 at 18:55
Add a comment
|
1 Answer
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.
5 Comments
Roko C. Buljan
Instead of
jpg|jpeg you can do: jpe?gMicah Engle-Eshleman
How about adding
webp to this list?Ala Eddine Menai
Could we consider
.ico as na image ?Woww
How would this look like when you also want to include capitalised extensions? And thank you for your answer
törzsmókus
@Woww the
/i part makes it case insensitive, i.e. include capitalised