Skip to content
Advertisement

js: how to intercept the suffix of domain name

I need to intercept the suffix in the domain name. How can I intercept the content between the last letter and the first /

let str1 = 'https://mail.xxx.net/main/';
// net

let str2 = 'https://blog.xxx.me/';
// me

let str3 = 'https://xxx.blog.com/home';
// com

let str4 = 'https://xxx.blog/home/#container';
// blog



console.log(`${str1}n${str2}n${str3}n${str4}n`)

Advertisement

Answer

You can do this using a simple regex:

const url = 'https://xxx.blog/home/#container';

const domain = url.match(/^https?://.+.([^/]+)/.*/);

console.log(domain[1]);
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement