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!!!