Goal would be to get one subdomain and swap it out for another. I feel there has to be a cleaner way to do this. help me out!
var url = window.location;
// example doamin would have a subdomain so https://auth.domain.ext
var domain = url.host;
var domainParts = domain.split('.');
var mainDomainParts = domainParts.slice(domainParts.length - 2, domainParts.length);
mainDomainParts.unshift('admin');
var adminDomain = mainDomainParts.join('.');
var fullAdminDomain = url.protocol + '//' + adminDomain;
Top comments (4)
This is using a regex, but at least it is a small one.
^
[^
.
.
]
+
.
)\.
.
gorgeous! I love me some regex. I was trying to do this yesterday but going about it all wrong. I was trying to abstract out the last part of the domain. Should have made it easy on myself and just find/replace the subdomain! Thank you!
Well, according to the documentation for slice, using a negative index will allow you start indexing from the end of the array rather than the beginning.
So
domainParts.slice(domainParts.length - 2, domainParts.length)
is equivalent todomainParts.slice(-2)
.Another thing is that since admin is a constant, you can add that when you are concatenating the string rather than manipulating the array.
Array.prototype.slice
aww yes! nice!