Getting the domain name out of a url

Hi there,

Here I am again, with regex:

I wrote this function to get the domain name out of a url. This should be the result I want:

Example:

http://github.com/assembly21/javascript -----> github
google.com ----> google
www.sitepoint.com ----> sitepoint

Here’s the function:

function domainName(url){
 var urlPattern = /^(?:https?:\/\/)?(?:w{3}\.)?([a-z\d\.-]+)\.(?:[a-z\.]{2,10})(?:[/\w\.-]*)*/;
 var domainPattern = url.match(urlPattern);
 var extractDomain = domainPattern[1];
 return extractDomain;
}

It works fine in the console, however it fails a test which tells me:

It returned google.co but should have been google.

I tested the regex here: https://regex101.com/#javascript. And that should be fine.

Deconstructed it is:

^(?:https?://)? —> is there an http or https part? don’t capture them.

(?:w{3}.)? —> are there 3 w? don’t capture them

([a-z\d.-]+). —> the domain name part. Capture it.

(?:[a-z.]{2,10})(?:[/\w.-]) —> the directories and folders part. don’t capture them.

I’m guessing it’s the way I’m extracting the domain name part, that’s not ideal?

Thanks a lot!!!

1 Like

i took the dot out of the domain part of the expression and it worked. thanks a lot anyway.

1 Like

For the domain part you want everything before the next /

There are characters allowed in domain names that you haven’t catered for

It’s a simple exercise and that’s what they want, but what characters would you allow? Also, if a . was allowed in the domain name, then it doesn’t work. How should I go about this?
Thanks!

Is this any help?

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.