Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    May 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    regex to get referring domain

    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.
    Code:
    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.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Code:
    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.
    Last edited by Philip M; 05-30-2012 at 08:11 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •