10 examples of 'get domain name from url javascript' in JavaScript

Every line of 'get domain name from url javascript' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
24function domain(url) {
25 url = url.replace(/.*\/\//, "");
26 url = url.replace(/\/.*/, "");
27 url = url.replace(/:.*/, "");
28 return url;
29}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

76function getDomainOfUrl(url) {
77 var domain = url.substring(url.indexOf('/')+2);
78 var slashIndex = domain.indexOf("/");
79 return (slashIndex !== -1 ? domain.substr(0,slashIndex) : domain);
80}
23function getDomain(url){
24 var t=url.split('://')[1];
25 return t.split('/')[0];
26}
14export function getDomain (url) {
15 return url.host.split('.').slice(-2).join('.')
16}
23function extractDomainFromUrl(url){
24 var domain_with_path = url.replace(/^https?:\/\//i, '');
25 var domain = domain_with_path.replace(/\/.*$/, '');
26 domain = domain.replace(/^www\./i, '');
27 return domain;
28}
6function subDomains(url) {
7 const subDomainsPtrn = /\$\{u:([\w-_.|]+)\}/.exec(url);
8
9 if (!subDomainsPtrn) {
10 return url;
11 }
12
13 const subDomainsList = subDomainsPtrn[1].split('|');
14
15 return url.replace(subDomainsPtrn[0], subDomainsList[(subDomainsCount++) % subDomainsList.length]);
16}
32public static getDomain(url: string): string {
33 let urlParts = url.split('/');
34 return urlParts[(urlParts.length - 2)];
35}
151function getDomain(referer) {
152 if (referer) {
153 let domainA = document.createElement('a');
154 domainA.href = referer;
155 return domainA.hostname;
156 }
157}
29export function getDomain(
30 url: string,
31 options: Partial<IOptions> = {},
32): string | null {
33 resetResult(RESULT);
34 return parseImpl(url, FLAG.DOMAIN, suffixLookup, options, RESULT).domain;
35}
205export function getGeneralDomainMinusTLD(url) {
206 if (url.hostIsIp) {
207 return 'IP';
208 }
209 // when domainInfo.domain is null, the hostname does not match a known public suffix (e.g. a
210 // local network hostname).
211 if (!url.domainInfo.domain) {
212 return url.domain;
213 }
214 return url.generalDomain.substring(0,
215 url.generalDomain.length - url.domainInfo.publicSuffix.length - 1);
216}

Related snippets