Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript regex URL matching

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...

like image 960
BonjourHolaOla Avatar asked May 15 '10 00:05

BonjourHolaOla


People also ask

Can we use regex in URL?

URL regular expressions can be used to verify if a string has a valid URL format as well as to extract an URL from a string.

Which of these regex is correct to match https or URL?

regex = “((http|https)://)(www.)?

How do you check a URL is valid or not in JavaScript?

You can use the URLConstructor to check if a string is a valid URL. URLConstructor ( new URL(url) ) returns a newly created URL object defined by the URL parameters. A JavaScript TypeError exception is thrown if the given URL is not valid.

How do I validate a link?

To check links at any other time, select the Links - Validate Links menu item or select Validate Links from the Page Tools left panel. CommonSpot differentiates between link types, validating http and verifying syntax for https and mailto.


1 Answers

Try this:

if(tab.url.match(/http\:\/\/www\.example\.com\/version\.php/i)) 
{
    //...
}
like image 145
limc Avatar answered Oct 30 '22 00:10

limc