0

I was helped on here some time ago in writing a regular expression to compare the location of the current page to that regex, and taking action if it's something specific. An example of that code is:

var re1 = new RegExp('^http://([^\.]+)\.domain\.com/subpage(.*)$');

if(window.location.href.match(re1))
{
  // Do more
}

In this case, I could write some code that would only execute on that subpage. It's worked beautifully so far. But I've run into a problem where I need further assistance with regular expressions.

Imagine a site like this: http://websitenamepreview.testserver.designcompanyname.com/subpage

How can I adapt this code to work on such a URL? The only thing that will probably change, so long as it's run from this test server, is the subpage.

user1729506
  • 975
  • 4
  • 15
  • 28

1 Answers1

1

Using you original code, you could change it to ^http://([^\.]+\.)+domain\.com/subpage(.*)$

Of course, for this particular test data, it wouldn't match, because you don't have "domain.com" as the end of the domain in your test URL.

If you can give us a little more information about what parts are important, what parts are likely to change in the data that you's be matching, etc. we could probably make it even less complex.

talemyn
  • 7,822
  • 4
  • 31
  • 52
  • For this test server, the URL all the way up to .com will probably never change. Just the subpage. I just need to check against any possible /page/page/page after .com, really. – user1729506 Mar 01 '13 at 21:06
  • In that case, you could probably get away with `^http://[^/].+/(.*)$`. That captures anything after the first "/" in the URL. – talemyn Mar 01 '13 at 21:16