52

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

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
prcaen
  • 2,429
  • 4
  • 25
  • 32

1 Answers1

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.

g13n
  • 3,218
  • 2
  • 24
  • 19