-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract-domain.js
More file actions
31 lines (27 loc) · 840 Bytes
/
Copy pathextract-domain.js
File metadata and controls
31 lines (27 loc) · 840 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*
Write a function that when given a URL as a string, parses out just the domain name and returns it as a string. For example:
* url = "http://github.com/carbonfive/raygun" -> domain name = "github"
* url = "http://www.zombie-bites.com" -> domain name = "zombie-bites"
* url = "https://www.cnet.com" -> domain name = cnet"
*/
function domainName(url) {
let substring = url;
if (url.includes('://')) {
substring = url.substring(url.indexOf('://') + 3);
}
if (url.includes('www.')) {
substring = url.substring(url.indexOf('www.') + 4);
}
if (substring.includes('.')) {
substring = substring.split('.')[0];
}
return substring;
}
// Refactored
function domainName(url) {
return url
.replace('http://', '')
.replace('https://', '')
.replace('www.', '')
.split('.')[0];
}