Skip to content

Instantly share code, notes, and snippets.

@erikvullings
Created October 23, 2022 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikvullings/bf60e814a751fe659ae9d8c91c70f4ff to your computer and use it in GitHub Desktop.
Save erikvullings/bf60e814a751fe659ae9d8c91c70f4ff to your computer and use it in GitHub Desktop.
Extract main domain name from a URL
const url = "https://www.lancasterguardian.co.uk/read-this/womens-world-cup-2023-groups-england-to-face-denmark-and-china-see-the-full-draw-3890113";
const url1 = "https://www.tudelft.nl";
const url2 = "https://whatsnew2day.com/world-cup-2023-draw-uswnt-face-vietnam-netherlands-playoff-winner-new-zealand-htmlns_mchannelrssns_campaign1490ito1490/";
const url3 = "https://www.news4jax.com/news/politics/2022/10/22/weapons-shortages-could-mean-hard-calls-for-ukraines-allies/";
const extractAgency = (url = '') => {
if (url.startsWith('https://t.me/')) return 'TELEGRAM';
let hostname: string;
try {
const u = new URL(url);
hostname = u.hostname || '';
} catch {
console.log('EXCEPTION for url ' + url)
hostname =
url
.replace(/https?:\/\//i, '')
.split(/[:/]/)
.pop() || '';
}
const parts = hostname.split('.');
const agency = (
parts.length <= 2 ? parts[0] : parts.length === 4 ? parts[1] : parts[parts.length - 2]
).toUpperCase();
return (isNaN(+agency)) ? agency : 'UNKNOWN';
};
console.log(extractAgency(url)); // LANCASTERGUARDIAN
console.log(extractAgency(url1)); // TUDELFT
console.log(extractAgency(url2)); // WHATSNEW2DAY
console.log(extractAgency(url3)); // NEWS4JAX
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment