views:

357

answers:

6

Ideally, I'd like to capture foo.com from something like http://foo.com/bar?param=value or https://www.foo.com

Thanks.

Edit: Sorry for the vagueness. I'll be doing this in Javascript.

2nd Edit: I should post my terrible first attempt: https?://w?w?w?[.]?[^?/]

A: 

If you're really sold on using regular expressions rather than built-in tools in the language (e.g. PHP's parse_url function) then something like:

(?:(\w+)://)([^/]+)(/\w+)?

will do it in a very rough way, with the hostname in subgroup 2. This is just a quick-and-dirty hack though (and won't work in all cases), and I would advise using the language's built-in tools.

If it's Javascript and the current URL, look at window.location.host or window.location.hostname.

Dave
+1  A: 

In Javascript:

http://gunblad3.blogspot.com/2008/05/uri-url-parsing.html

Robert Harvey
Since I use jQuery a lot more than prototype, I've also found http://projects.allmarkedup.com/jquery_url_parser/
Udbhav
+1  A: 

If doing this in JavaScript, you don't even need a regex. You can use the location object like so:

var host = window.location.host;

This link has more information about how to get other parts of the URL: http://www.comptechdoc.org/independent/web/cgi/javamanual/javalocation.html.

Bialecki
+12  A: 

You don't need regex for this! Let the browser parse it for you:

var a = document.createElement('a');
a.href = 'http://foo.com/bar?param=value';
a.hostname; // "foo.com"

Voila!

Crescent Fresh
+1  A: 

For full cross-browser compatibility simply go with the document object, not window:

var theHost = document.location.host;

drlouie - louierd