How can I use jQuery to match a string inside the current URL of the window I am in?

Posted by Jannis on Stack Overflow See other posts from Stack Overflow or by Jannis
Published on 2010-03-26T10:35:47Z Indexed on 2010/03/26 10:43 UTC
Read the original article Hit count: 448

Hi,

I have used the excellent gskinner.com/RegExr/ tool to test my string matching regex but I cannot figure out how to implement this into my jQuery file to return true or false.

The code I have is as follows:

^(http:)\/\/(.+\.)?(stackoverflow)\.

on a url such as http://stackoverflow.com/questions/ask this would match (according to RegExr) http://stackoverflow.

So this is great because I want to try matching the current window.location to that string, but the issue I am having is that this jQuery/js script does not work:

var url = window.location;
if ( url.match( /^(http:)\/\/(.+\.)?(stackoverflow)\./ ) ) 
{
    alert('this works');
};

Any ideas on what I am doing wrong here?

Thanks for reading.

Jannis

© Stack Overflow or respective owner

Related posts about regex

Related posts about jQuery