Reputation: 975
I have two pages, each made up of divs
that contain location info. Each page currently displays all of the locations in the database, but each page needs to only display locations for one of the two cities. I need to remove all irrelevant locations from each of the two pages. Since I can't put a diff script on each page, I have to differentiate between page location / url.
One of the two cities is Minneapolis, so an example of what I am trying as a regular expression:
new RegExp('^http://([^\.]+)\.domain\.com/contact-us/minneapolis-locations(.*)$');
How can I write an IF
or other statement to check the page location against this regular expression? Thanks!
Upvotes: 0
Views: 632
Reputation: 4024
Not quite sure I'm understanding your question. You want to test the current url against that regular expression in an if statement?
var reg = new RegExp('^http://([^\\.]+)\\.domain\\.com/contact-us/minneapolis-locations(.*)$');
if (location.href.match(reg))
{
...
}
or using a regex literal:
var reg = /^http:\/\/([^\.]+)\.domain\.com\/contact-us\/minneapolis-locations(.*)$/;
if (location.href.match(reg))
{
...
}
Upvotes: 1
Reputation: 7786
Use the String#match(). It will return an array of matches if any, or null if none exist.
var re = new RegExp('^http://([^\.]+)\.domain\.com/contact-us/minneapolis-locations(.*)$');
if(window.location.href.match(re)){
//do something
}
Upvotes: 2