下面是我的网址,我只想要子域名,即dnyaneshtechno
网址:https://dnyaneshtechno.sharepoint.com
根据上面的网址,我只想获取'dnyaneshtechno‘,我正在使用angular 8作为前端,所以有人可以在这方面帮助我。
发布于 2020-10-28 14:47:47
您可以使用split和substr函数或从url获取subdomain
示例:
动态链接:
let getLink = window.location.href ;
console.log(getLink) // dynamic link
var subdomain = getLink.substr(COUNT, 1000).split(".")[0]使用拆分方法的:
var Link = "https://SUBDOMAIN.DOMAIN.com"
var subdomain = Link.substr(COUNT, 1000).split(".")[0]
console.log(subdomain) //SUBDOMAIN或使用get subdomain form link的
const { hostname } = new URL('https://SUBDOMAIN.DOMAIN.com')
const [subdomain] = hostname.split('.')
console.log(subdomain) //SUBDOMAIN我希望我能对大家有所帮助!
发布于 2020-10-28 14:40:44
假设您想要从字符串中提取子域,这不是一个角度问题,而是纯JavaScript。有一个URL构造函数可以做到这一点:
const {
hostname
} = new URL('https://dnyaneshtechno.sharepoint.com')
const [subdomain] = hostname.split('.')
console.log(subdomain)
发布于 2020-10-28 14:37:49
您可以使用substr和拆分函数,即。
var a = "https://dnyaneshtechno.sharepoint.com"
var subdomain = a.substr(9, 1000).split(".")[0]https://stackoverflow.com/questions/64567522
复制相似问题