Skip to content

Instantly share code, notes, and snippets.

@kibolho
Last active March 9, 2023 21:10
Show Gist options
  • Select an option

  • Save kibolho/a151914c69feb4440729ebef796febad to your computer and use it in GitHub Desktop.

Select an option

Save kibolho/a151914c69feb4440729ebef796febad to your computer and use it in GitHub Desktop.
Regex to get subdomain
const url = "painel.toloja.com.br";
const regex = url.match(/[a-z]+\.?(([a-z]+)\.(([a-z]+)\.[a-z]+\.[a-z]+))[:\d]*/);
const mainUrl = regex ? (regex[2]=== "dev" ? regex[1]: regex[3]) : "defaultOrganizationUrl";
const newOrganization = regex ? regex[4] : "defaultOrganization";
console.log(regex);
console.log(mainUrl);
console.log(newOrganization);
const url = "painel.dev.toloja.com.br";
const regex = url.match(
/(.*)+\.([a-z]+)?\.?(([a-z]+)\.(([a-z]+)))[:\d]*/
)
let newOrganization = 'demo-ria-admin'
if (regex && regex?.length >= 5) {
newOrganization = regex[1].includes('.')
? regex[1].split('.')[0]
: regex[1]
}
console.log(regex);
console.log(newOrganization);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment