Skip to content

Instantly share code, notes, and snippets.

@pgilad
Last active October 10, 2018 11:55
Show Gist options
  • Save pgilad/9660929 to your computer and use it in GitHub Desktop.
Save pgilad/9660929 to your computer and use it in GitHub Desktop.
Regex for matching subdomains in document.location.hostname
var getSubdomain = function (baseDomain) {
if (!baseDomain) {
return null;
}
var regSub = new RegExp('^((?!' + baseDomain + '|www)([^.]*))', 'g');
var _subdomain = document.location.hostname.match(regSub);
if (_subdomain && _subdomain[0]) {
return _subdomain[0];
} else {
return null;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment