-
Notifications
You must be signed in to change notification settings - Fork 0
/
urlExtract.js
39 lines (25 loc) · 1.05 KB
/
urlExtract.js
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
32
33
34
35
36
37
38
39
//Write a function that when given a URL as a string,
//parses out just the domain name and returns it as a string
function domainName(url){
// get url
// check for https:// , https://www. & www. then remove any of those
// return substring
const dot = (url) => url.indexOf('.') // too lazy to type that out more than once
// check for www. and remove up to .
if (url.includes('www.')) {
const less = url.substring(dot(url) + 1) // + 1 to not include the .
return less.slice(0, dot(less)) // go from the "second" but now first .
} else {
// check for http or https
if (url.startsWith('https://')) {
const less = url.slice(8)
return less.slice(0, dot(less))
} else if (url.startsWith('http://')) {
const less = url.slice(7)
return less.slice(0, dot(less))
}
}
// return if theres no prefix
return url.slice(0, dot(url))
}
domainName('https://google.co.jp')