Remove everything after domain and http in javascript url

I have the full url in a javascript variable and I want to split it into an empty url.

For instance:

https://www.google.com/hello/hi.php , http://youtube.com

strip down:

www.google.com, youtube.com

How do I do this in javascript?

thanks

This is not like other links as I am doing this inside the chrome extension, so the only way to get the url is to use the chrome extension which only provides the full url. So I need to remove the full URL

+4
source share
2 answers

You can try the following:

\/\/([^\/,\s]+\.[^\/,\s]+?)(?=\/|,|\s|$|\?|#)

Regex live here.


Live JavaScript example:

 var regex = /\/\/([^\/,\s]+\.[^\/,\s]+?)(?=\/|,|\s|$|\?|#)/g; var input = "https://www.google.com/hello/hi.php, http://youtube.com," + "http://test.net#jump or http://google.com?q=test"; while (match = regex.exec(input)) { document.write(match[1] + "<br/>"); }; 

Hope this helps

+2
source

Use the global variable window.location.hostname and it will provide you with this information.

+3
source

All Articles