View Full Version : regex to get referring domain

05-30-2012, 07:05 PM
I am trying to check the referring domain and match certain domains and got part way there but not all of the way.

Ideally I want to check referrer and if it matches google, yahoo, bing, msn, etc. I want to get which one so I can change some stuff on the site for them.

So far I have something like this.

var url = document.referrer;
patternstring = /^https?:\/\/(\w+\.)?(google|yahoo|msn)\./;
matcher = url.match(patternstring);

This will match but gives back more than I want. I would only want it to give back google or yahoo or msn.

Any help would be appreciated.

Philip M
05-30-2012, 07:59 PM
patternstring = /^(https?:\/\/)(\w+\.)?(google|yahoo|msn)\./;
matcher = url.match(patternstring);
if (matcher !=null) { // a match was found
alert (matcher[1]); // http://
alert (matcher[2]); // www.
alert (matcher[3]); // google or yahoo or msn

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.