Regex to match Image Url

自古美人都是妖i 提交于 2019-12-10 07:50:04

问题


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 ?


回答1:


do you mean:

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



回答2:


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

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



回答3:


You are missing ! in the if condition

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

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



来源:https://stackoverflow.com/questions/8834813/regex-to-match-image-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!