如何从网址获取域名
嗯......问题出在第一部分。 这是正确的代码(将拆分www和。)
export function extractHostname(url: string): string { let hostname; // remove protocol if (url.indexOf('//') > -1) { hostname = url.split('/')[2]; } else { hostname = url.split('/')[0]; } if (url.indexOf('www.') > -0) { hostname = url.split('www.')[1]; } hostname = hostname.split('.')[0]; // remove port hostname = hostname.split(':')[0]; // remove query hostname = hostname.split('?')[0]; return hostname; }