最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript regex URL matching - Stack Overflow

programmeradmin4浏览0评论

I have this so far:

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.example\/version.php/i')) 
    {
        document.getElementById('link').innerHTML = '<p>' + btn + '</p>';
    }
});

Basically it should match the domain within this:

.php?*

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

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.

EDIT:

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

Doesn't even work somehow...

I have this so far:

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.example.com\/version.php/i')) 
    {
        document.getElementById('link').innerHTML = '<p>' + btn + '</p>';
    }
});

Basically it should match the domain within this:

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

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

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.

EDIT:

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

Doesn't even work somehow...

Share Improve this question edited Oct 17, 2020 at 15:11 Brian Tompsett - 汤莱恩 5,88372 gold badges61 silver badges133 bronze badges asked May 15, 2010 at 0:23 BonjourHolaOlaBonjourHolaOla 1353 gold badges4 silver badges8 bronze badges 0
Add a comment  | 

4 Answers 4

Reset to default 7

Try this:

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

match() should take a RegExp object as argument, not a string. You could probably just remove the single quotes '' from around the expression to make it work. For future reference, you should also escape the periods . in the expression (as it is they will match any single character), and insert a ^ in the beginning to only allow this match in the beginning of the URL.

Remove the quotes. Opera DargonFly gives me:

>>> 'http://www.example.com/version.php'.match(/^http:\/\/www\.example.com\/version\.php/i) 
[object Array]

You are creating the RegExp object, but you're not matching it against anything. See here for how to use it (by the way, the syntax is also wrong, it's either /expression/modifiers or RegExp(expression, modifiers)).

发布评论

评论列表(0)

  1. 暂无评论