0

I want to select all the a elements which point to external links, so I want a selector which picks only the href values matched with

/https?:\/\/[^\/\!\?\<\>\_\*\&\(\)\#\$\@]*/

that are different from

window.location.hostname

what's the correct expression?

untore
  • 603
  • 8
  • 16

3 Answers3

0
$("a:not([href*="+window.location.hostname+"])[href*=http], a:not([href*="+window.location.hostname+"])[href*=https]")

Will give you all the links not having window.location.hostname but having http or https

void
  • 36,090
  • 8
  • 62
  • 107
0

This would be more robust solution, checking for different location hostname than main window:

var $externalLinks = $('a[href]').filter(function(){
    return this.hostname != window.location.hostname;
});

-jsFiddle-

A. Wolff
  • 74,033
  • 9
  • 94
  • 155
  • @Vohuman Thx but not sure why this has been downvoted, i must be missing something obvious, maybe...?! – A. Wolff Feb 15 '15 at 15:43
  • There is nothing wrong with the answer. Sometimes the reason of downvote is not _technical_! – Ram Feb 15 '15 at 15:49
  • maybe the answer does not resolves around regex but it solves the problem i was going to use regex for – untore Feb 15 '15 at 16:18
-2

No regex needed. Try this (using Jquery):

var links = $('a[href^="http"]');
Marc Anton Dahmen
  • 1,071
  • 6
  • 6