-16

How to validate image file extension (jpg,png,gif,bmp) with JavaScript regular expression?

1

1 Answer 1

11

Accordingly your issue the regular expression is quite simple.

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

Do you really sure that nothing else will be used? For example, JPEG format allows both .jpg and .jpeg extensions. That's why I put e? pattern in the regular expression.

Example of validation could be as follows:

var filename = "/site/images/test.png";
if ( /\.(jpe?g|png|gif|bmp)$/i.test(filename) ) {
. . .

.

Sign up to request clarification or add additional context in comments.

1 Comment

yes this is the regex, but how to validate the url, as example I have this path "/site/images/test.png". how to validate it ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.