正则表达式匹配图像 URL

发布于 2024-12-26 06:31:19 字数 478 浏览 1 评论 0原文

我正在尝试获取一个简单的正则表达式来验证 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/

发生了什么事?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

娇柔作态 2025-01-02 06:31:19

你的意思是:

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

do you mean:

if (!testRegex.test(imageUrl)) {
  alert('Not Match');
}
纸短情长 2025-01-02 06:31:19

如果 testRegex 匹配,testRegex.test 将计算为 True。
IE

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

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

if (testRegex.test(imageUrl)) {
  alert('Match');
} else {
  alert('Not match');
}
情绪失控 2025-01-02 06:31:19

if 条件中缺少 !

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

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

You are missing ! in the if condition

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文