Help with href.match...

Help with href.match...

I am using this snippet of code on one of the websites that I maintain daily for my job and I've come across an issue that I can't seem to debug/solve myself.

Using this code:
  1. // link automations
    var external_re = new RegExp('^https?://(?!'+document.location.hostname+')/');

    $('a').each(function(){
        // is this an external link?
        if (this.href.match(external_re)) {
            if (!this.target) this.target = '_blank';
        }
    });







This basically gives all <a> elements a target of "_blank" that aren't linked within the document.location.hostname.  However, whenever I click on a link that is in a separate folder from the root, i.e. /news/, the jQuery doesnt seem to work on external links. 

Does anybody have any idea why or have any idea how to solve this?

Thanks in advance!

Adam