-2

My Url looks like:

http://mywebsite/series/2545-abc

But I don't know how to check URL with regex. So, How to check url current windows with above URL?

I mean:

If Url = http://mywebsite/series/2545-abc
  do something

1 Answers1

1

What about this? The regular expression defined at the first line is tested against the URL:

var myRe = /\/series\//; //regular expression to use
if(myRe.test(window.location.href)){
    alert("matching");
}else{
    alert("not matching");
}

If you want to test for the whole URL (and really want to use a regular expression for that), you could replace the first line with

var myRe = /^http:\/\/mywebsite\/series\/2545-abc$/;

If you remove the dollar sign at the end, modifications at the end of the URL are accepted as well (e.g. http://mywebsite/series/2545-abc/foo.html)

Zain
  • 25,701
  • 7
  • 36
  • 61
muffel
  • 6,359
  • 5
  • 51
  • 86