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 ?


你的意思是:

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

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

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

You are missing ! in the if condition

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

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

链接地址: http://www.djcxy.com/p/70250.html

上一篇: 如何在jQuery中获取当前页面路径(在做Ajax调用时)

下一篇: 正则表达式来匹配图片网址