Link to home
Start Free TrialLog in
Avatar of vreten
vreten

asked on

Get site domain from href with regex

Hi, i would like to compose a regex that extracts site.domain from a href element. I.e.
if :
* href = 'http://www.domainA.com/a?a=b'
or
* href = 'http://www.domainA.com'
or
* href = 'www.domainA.com/a?a=b'
or
* href = 'domainA.com/a?a=b'
or
* href = 'www.domainA.com/a?a=b'
i want the regex to return domainA.com.

As an example:
href = 'http://www.domainA.com/a?a=b'
domain = href.match(/[\w\$\-\_\+\!\*]+\.([\w\$\-\_\+\!\*]+\.)*[\w\$\-\_\+\!\*]+[^\/]/);

returns domain = 'www.domainA.com'

But... as i said.... I want href to be 'domainA.com' instead.

With my limited knowledge of regex i can't seem to get the expression right. I do understand that i easily could do the same thing with a couple of simple string manipulations. But that is simply not the way I whant to do it :)
Avatar of hielo
hielo
Flag of Wallis and Futuna image

try:
<script type="text/javascript">
href = 'http://www.domainA.com/a?a=b'
re = /^https?:\/\/([^/]+)?/i;
href.match( re );
alert( RegExp.$1 )
</script>

Open in new window

Avatar of vreten
vreten

ASKER

Yepp, short and elegant...but the problem is that this still returns
'www.domainA.com' and not just 'domainA.com' . If you can tweek that just a bit i would be most greatful :)
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vreten

ASKER

short and nice, thank you! Wondering if I  have to handle the case where no http:// exists on link...but i reccon that will not happen due to w3c restrictions...or? If no http:// will FF & IE treat link as relative?
If you have:
<a href="?php.php">

 then it is treated as a relative url and you will need to get the domain from window.location.href