Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strip domain name from url (string)

Tags:

jquery

string

I'm accessing the stylesheet collection like this:

var css = document.styleSheets[0];

It returns eg. http://www.mydomain.com/css/main.css

Question: how can I strip the domain name to just get /css/main.css ?

like image 786
Fuxi Avatar asked Apr 08 '10 01:04

Fuxi


People also ask

How do I find the domain of a string?

First let's create a string with our URL (Note: If the URL isn't correctly structured you'll get an error). const url = 'https://www.michaelburrows.xyz/blog?search=hello&world'; Next we create a URL object using the new URL() constructor. let domain = (new URL(url));

How do I extract a domain name?

To extract a domain name from a URL, first remove the protocol (http://, https://, ftp://, etc.), then remove any subdomains (www., blog., etc.), then remove the top-level domain (.com, . net, . org, etc.).


1 Answers

This regular expression should do the trick. It will replace any domain name found with an empty string. Also supports https://

//css is currently equal to http://www.mydomain.com/css/main.css    
css = css.replace(/https?:\/\/[^\/]+/i, "");

This will return /css/main.css

like image 138
Erikk Ross Avatar answered Sep 27 '22 21:09

Erikk Ross