Last active
March 9, 2023 21:10
-
-
Save kibolho/a151914c69feb4440729ebef796febad to your computer and use it in GitHub Desktop.
Regex to get subdomain
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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