Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Get site domain from href with regex

Posted on 2008-06-09
5
Medium Priority
?
2,194 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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 code hide show and toggles. 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…

661 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