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 :)
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\$\-\_\+\!\
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 :)
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 :)
'www.domainA.com' and not just 'domainA.com' . If you can tweek that just a bit i would be most greatful :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
<a href="?php.php">
then it is treated as a relative url and you will need to get the domain from window.location.href
Open in new window