Javascript正则表达式URL匹配 [英] Javascript regex URL matching

查看:128
本文介绍了Javascript正则表达式URL匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我到目前为止:

chrome.tabs.getSelected(null, function(tab) 
{
    var title = tab.title;
    var btn = '<a href="' + tab.url + '" onclick="save(\'' + title + '\');"> ' + title + '</a>';

    if(tab.url.match('/http:\/\/www.mydomain.com\/version.php/i')) 
    {
        document.getElementById('link').innerHTML = '<p>' + btn + '</p>';
    }
});

基本上它应匹配此域内的域名:

Basically it should match the domain within this:

http://www.mydomain.com/version.php?*

任何匹配的东西,即使它包含类似version.php?ver = 1等的东西

Anything that matches that even when it includes something like version.php?ver=1, etc

当我使用上面的代码时,它没有显示任何内容,但是当我删除if语句时,它很好,但它在其他页面上显示它不应该匹配的URL。

When I used the code above of mine, it doesn't display anything, but when I remove the if statement, it's fine but it shows on other pages which it shouldn't only on the matched URL.

编辑:

if(tab.url.match(/http:\/\/www.mydomain.com\/version.php/i)) 
{
    document.getElementById('link').innerHTML = '<p>' + btn + '</p>';
}

甚至不能以某种方式工作......

Doesn't even work somehow...

推荐答案

试试这个

if(tab.url.match(/http\:\/\/www\.mydomain\.com\/version\.php/i)) 
{
    //...
}

这篇关于Javascript正则表达式URL匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆