Skip to content Skip to sidebar Skip to footer

Javascript Regex URL Matching

I have this so far: chrome.tabs.getSelected(null, function(tab) { var title = tab.title; var btn = ' ' +

Solution 1:

Try this:

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

Solution 2:

Remove the quotes. Opera DargonFly gives me:

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

Solution 3:

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


Solution 4:

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.


Post a Comment for "Javascript Regex URL Matching"