I'm trying to come up with a Javascript RegEx command that will convert these inputs into the following outputs:
INPUT DESIRED OUTPUT
mydomain.com --> mydomain.com
foo.mydomain.com --> mydomain.com
dev.mydomain.com --> dev.mydomain.com
dev-foo.mydomain.com --> dev.mydomain.com
Here's the rules:
- Remove the subdomain from the domain unless the subdomain starts with the characters "dev"
- If the subdomain starts with "dev", then output "dev.domain.com"
- Retain any port specified at the end of the domain
My regex skills are failing me. This is what I have so far:
'mydomain.com'.replace(/^(dev)?(-.*)?(mydomain.com)/,'$1.$3');
// .mydomain.com
'foo.mydomain.com'.replace(/^(dev)?(-.*)?(mydomain.com)/,'$1.$3');
// foo.mydomain.com
'dev-foo.mydomain.com'.replace(/^(dev)?(-.*)?(mydomain.com)/,'$1.$3');
// dev.mydomain.com
'dev.mydomain.com'.replace(/^(dev)?(-.*)?(mydomain.com)/,'$1.$3');
// dev.mydomain.com
The first two fail and the last two work. Any suggestions?
Here's javascript that works, but I was hoping to combine it into a single regex replace command. Note that I also want to retain any port specified at the end of the dmain.
var getHost = function () {
var host = window.location.host;
if (host.substring(0,3) === 'dev') {
return host.replace(/^(dev)?(-.*)?(mydomain\.com.*)/,'$1.$3');
}
else {
return 'mydomain.com';
}
}
mydomain.comor(something).mydomain.comand should always returnmydomain.com, unless(something)contains the word 'dev', in which case it should bedev.mydomain.com?? Or you just want to remove any occurences offoo?