Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RegEx to extract just domain name from URL?

Status
Not open for further replies.

blove57

Programmer
Nov 27, 2007
6
0
0
US
I need to grab just the domain name from the URL the user is requesting. I need to make sure the domain is only return since my application uses this info to differentiate different domain names pointing to the same web application. For example, if the URL is:

mydomain.com:8080
abc.mydomain.com?param1=123
xyz.mydomain.com:5000

I need all these to return "mydomain.com". What's the best way to do this?
 
You may use this as a start (off-hand without taking careful considerations of spec of a general url).
[tt]
var rx,s,t;
s="abc.mydomain.com?param1=123"; //etc
rx=/^([^.]*?\.)?(\w+\.\w+)([:|?].*?)?$/;
//result is t: if s is not in legitimate form, t returns empty
t=(rx.test(s))?s.replace(rx,"$2"):"";
[/tt]
The above is only meant to show how to exercise the mental process for the match... I am sure somewhere out there you should find more specific (such as more focused character strings instead of \w+) format. It would suffice to google for it. Also the two components mydomain.com certainly won't be good enough for a general application.
 
another option may be to use the SPLIT function to seperate the data out by splitting on the . then you can get the bits you need e.g.

myDNS="myArray=myDNS.split(".")

returns :

myArray[0]="www"
myArray[1]="mydomain"
myArray[2]="com"

Greg Griffiths
Livelink Certified Developer & ECM Global Star Champion 2005 & 2006
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top