Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2200
  • Last Modified:

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 :)
0
vreten
Asked:
vreten
  • 3
  • 2
1 Solution
 
hieloCommented:
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

0
 
vretenAuthor Commented:
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 :)
0
 
hieloCommented:
try this:
<script type="text/javascript">
href = 'http://www.domainA.com/a?a=b'
re = /^https?:\/\/(www\.)?([^/]+)?/i;
href.match( re );
alert( RegExp.$2 )
</script>

Open in new window

0
 
vretenAuthor Commented:
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?
0
 
hieloCommented:
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
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now