[JS] Extract Hostname From URL STRING
Extract Hostname/Domain from URL (string)
function extractHostname(url) { var hostname; //find & remove protocol (http, ftp, etc.) and get hostname if (url.indexOf("//") > -1) { hostname = url.split('/')[2]; } else { hostname = url.split('/')[0]; } //find & remove port number hostname = hostname.split(':')[0]; //find & remove "?" hostname = hostname.split('?')[0]; return hostname; } //test the code Press F12 console.log("== Testing extractHostname: =="); console.log(extractHostname("http://www.blog.classroom.me.uk/index.php")); console.log(extractHostname("http://www.youtube.com/watch?v=ClkQA2Lb_iE")); console.log(extractHostname("https://www.youtube.com/watch?v=ClkQA2Lb_iE")); console.log(extractHostname("www.youtube.com/watch?v=ClkQA2Lb_iE")); console.log(extractHostname("ftps://ftp.websitename.com/dir/file.txt")); console.log(extractHostname("websitename.com:1234/dir/file.txt")); console.log(extractHostname("ftps://websitename.com:1234/dir/file.txt")); console.log(extractHostname("example.com?param=value")); console.log(extractHostname("https://facebook.github.io/jest/")); console.log(extractHostname("//youtube.com/watch?v=ClkQA2Lb_iE")); console.log(extractHostname("http://localhost:4200/watch?v=ClkQA2Lb_iE"));
0 Response to "[JS] Extract Hostname From URL STRING"
Post a comment
Jangan Lupa Cek Box "Notify Me" agar tahu komentar kamu dibalas oleh saya.
If there are any posts that are missing, or error or anything else, please leave a comment for the article / post to be fixed.
Do not Forget Check Box "Notify Me" to know our comments replied by me.