问题
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