Solved

Get site domain from href with regex

Posted on 2008-06-09
5
2,150 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

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 …
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

744 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

16 Experts available now in Live!

Get 1:1 Help Now