/** * Extracts only the website name from a url * @example "Twitter" from "https://twitter.com/example" * @param {string} url the link to extract the name from * @returns Website name */ function extractWebsiteName(url) { if (url) { const regex = /(?:https?:\/\/)?(?:www\.)?(?:m\.)?(?:([^./]+)\.)?([^./]+\.com)/; const matches = url.match(regex); const [, subdomain, domain] = matches; let socialMediaName = subdomain ? subdomain.charAt(0).toUpperCase() + subdomain.slice(1) : domain.charAt(0).toUpperCase() + domain.slice(1); socialMediaName = socialMediaName.replace(/\.[^/.]+$/, ''); return socialMediaName; // Remove the top-level domain } else { return ''; } } console.log(extractWebsiteName("https://www.mydomain.com/blog?search=hello&world")); // Mydomainvar url = window.location.href; var domain = url.replace('http://','').replace('https://','').split(/[/?#]/)[0]; const url = 'https://www.mydomain.com/blog?search=hello&world'; const domain = (new URL(url)).hostname.replace('www.','');window.location.hostnameurl = http://localhost:4200/landing?query=1#2 window.location.hash: "#2" window.location.host: "localhost:4200" window.location.hostname: "localhost" window.location.href: "http://localhost:4200/landing?query=1#2" window.location.origin: "http://localhost:4200" window.location.pathname: "/landing" window.location.port: "4200" window.location.protocol: "http:" window.location.search: "?query=1"function getDomainFromURL(url) { const parsedURL = new URL(url); return parsedURL.hostname; } const url = 'https://www.example.com/path?page=1'; const domain = getDomainFromURL(url); console.log(domain);const url = 'http://www.youtube.com/watch?v=ClkQA2Lb_iE'; const { hostname } = new URL(url); console.assert(hostname === 'www.youtube.com'); // truelet domain = (new URL(url)); domain = domain.hostname; console.log(domain); //www.example.com