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
?
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));
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.).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With