Ryan Ryan - 1 year ago 74
Javascript Question

How to parse a URL?

If there is one thing I just cant get my head around, it's regex.

So after a lot of searching I finally found this one that suits my needs:

function get_domain_name()
domain_name_parts = aaaa.match(/:\/\/(.[^/]+)/)[1].split('.');
if(domain_name_parts.length >= 3){
domain_name_parts[0] = '';
var domain = domain_name_parts.join('.');
if(domain.indexOf('.') == 0)
alert("1"+ domain.substr(1));
alert("2"+ domain);

It basically gives me back the domain name, is there anyway I can also get all the stuff after the domain name? in this case it would be
from the

Answer Source

Please note that this solution is not the best. I made this just to match the requirements of the OP. I personally would suggest looking into the other answers.

THe following regexp will give you back the domain and the rest. :\/\/(.[^\/]+)(.*):

  1. www.google.com
  2. /goosomething

I suggest you studying the RegExp documentation here: http://www.regular-expressions.info/reference.html

Using your function:

function get_domain_name()
        var matches = aaaa.match(/:\/\/(?:www\.)?(.[^/]+)(.*)/);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download