Solved

Get site domain from href with regex

Posted on 2008-06-09
5
2,158 Views
Last Modified: 2010-07-27
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
Comment
Question by:vreten
  • 3
  • 2
5 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 21744769
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
 

Author Comment

by:vreten
ID: 21744952
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
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 21745019
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
 

Author Closing Comment

by:vreten
ID: 31465464
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
 
LVL 82

Expert Comment

by:hielo
ID: 21745148
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now