正则表达式匹配图像URL

时间:2012-01-12 12:06:07

标签: regex url pattern-matching

我正在尝试使用简单的正则表达式来验证URL是否包含结尾数字

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');
}

应该触发alert('Not Match');但它不会?见http://jsfiddle.net/UgfKn/

发生了什么事?

3 个答案:

答案 0 :(得分:3)

你的意思是:

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

答案 1 :(得分:1)

如果testRegex.test匹配,

testRegex将评估为True。 即

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

答案 2 :(得分:0)

if条件

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

请参阅 http://jsfiddle.net/UgfKn/1/

相关问题