Check extension with RegEx

For example, I have a string /wiki/File:test.JPG, I have to check if it has one of the extensions"jpeg","jpg","png","gif"

I currently wrote that link.search(/.[j,p,g][p,i,n][e,g,f][g]?/gi), and it works, but I would like to improve the regex.

+3
source share
5 answers

Just write a regex as a list:

/\.(jpe?g|png|gif)$/gi

(note the escaped dot ( .))

EDIT: added $.

+8
source

Just for future reference, this tool is invaluable when working with regex in flash:

Web version:

http://gskinner.com/RegExr/

Desktop version:

http://gskinner.com/RegExr/desktop/

+4
source

, - ?

/\.(jpe?g|jpg|png|gif)$/gi

( ), $, , .

+2
 var pattern: RegExp = /\.(jpe?g|png|gif)$/gim
+1

If you also want a case-sensitive match, try the following:

 \.(?i)(jpe?g|png|gif)$

The point must be escaped, otherwise it will match any character :)

+1
source

All Articles