Skip to content

Instantly share code, notes, and snippets.

@stewartcelani
Created May 17, 2017 13:19
Show Gist options
  • Save stewartcelani/8642cc07a69fbcddf955311450be22db to your computer and use it in GitHub Desktop.
Save stewartcelani/8642cc07a69fbcddf955311450be22db to your computer and use it in GitHub Desktop.
Extract Root Domain
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
function extractDomain(url) {
return new Promise(function (resolve, reject) {
var domain;
//find & remove protocol (http, ftp, etc.) and get domain
if (url.indexOf("://") > -1) {
domain = url.split('/')[2];
}
else {
domain = url.split('/')[0];
}
domain = domain.split(':')[0];
return resolve(domain);
})
}
function extractDomainRoot(domain){
// Works back from end segment and returns first segment > 4 characters
// Dumb implementation as does not work for domains <= 4 characters but does the job asfar as augmenting
// ElasticSearch hits
let parts = domain.split('.')
for (let i = parts.length; i--; i >= 0) {
if (parts[i].length > 4) {
return parts[i]
}
}
};
const test = 'http://molstaging.elasticdesign.com.au'
extractDomain(test).then((domain) => {
console.log(extractDomainRoot(domain))
}).catch((err) => {
console.error(err)
})
</script>
<script id="jsbin-source-javascript" type="text/javascript">function extractDomain(url) {
return new Promise(function (resolve, reject) {
var domain;
//find & remove protocol (http, ftp, etc.) and get domain
if (url.indexOf("://") > -1) {
domain = url.split('/')[2];
}
else {
domain = url.split('/')[0];
}
domain = domain.split(':')[0];
return resolve(domain);
})
}
function extractDomainRoot(domain){
// Works back from end segment and returns first segment > 4 characters
// Dumb implementation as does not work for domains <= 4 characters but does the job asfar as augmenting
// ElasticSearch hits
let parts = domain.split('.')
for (let i = parts.length; i--; i >= 0) {
if (parts[i].length > 4) {
return parts[i]
}
}
};
const test = 'http://molstaging.elasticdesign.com.au'
extractDomain(test).then((domain) => {
console.log(extractDomainRoot(domain))
}).catch((err) => {
console.error(err)
})</script></body>
</html>
function extractDomain(url) {
return new Promise(function (resolve, reject) {
var domain;
//find & remove protocol (http, ftp, etc.) and get domain
if (url.indexOf("://") > -1) {
domain = url.split('/')[2];
}
else {
domain = url.split('/')[0];
}
domain = domain.split(':')[0];
return resolve(domain);
})
}
function extractDomainRoot(domain){
// Works back from end segment and returns first segment > 4 characters
// Dumb implementation as does not work for domains <= 4 characters but does the job asfar as augmenting
// ElasticSearch hits
let parts = domain.split('.')
for (let i = parts.length; i--; i >= 0) {
if (parts[i].length > 4) {
return parts[i]
}
}
};
const test = 'http://molstaging.elasticdesign.com.au'
extractDomain(test).then((domain) => {
console.log(extractDomainRoot(domain))
}).catch((err) => {
console.error(err)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment