Andy
Andy

Reputation: 19251

Regex to match Image Url

I am trying to get a simple regex to verify that a URL contains the ending figures

var testRegex = /^https?:\/\/(?:[a-z\-]+\.)+[a-z]{2,6}(?:\/[^\/#?]+)+\.(?:jpe?g|gif|png)$/;
var imageUrl = "http://stackoverflow.com/questions/406192/how-to-get-the-current-url-in-jquery";
if (testRegex.test(imageUrl)) {
  alert('Not Match');
}

And this should trigger alert('Not Match'); and it doesn't ? See http://jsfiddle.net/UgfKn/

What's going on ?

Upvotes: 4

Views: 8901

Answers (3)

Prashant Bhate
Prashant Bhate

Reputation: 11087

You are missing ! in the if condition

if ( ! testRegex.test(imageUrl1) ) {
    alert('Not Match');
}

See http://jsfiddle.net/UgfKn/1/

Upvotes: 0

itea
itea

Reputation: 444

do you mean:

if (!testRegex.test(imageUrl)) {
  alert('Not Match');
}

Upvotes: 3

mathematical.coffee
mathematical.coffee

Reputation: 56915

testRegex.test will evaluate to True if testRegex DOES match. ie

if (testRegex.test(imageUrl)) {
  alert('Match');
} else {
  alert('Not match');
}

Upvotes: 1

Related Questions