Retrieve substring

I have URLs as follows.

http://mysite.company.com/webApp1/site1/Pages/default.aspx
https://mysite2.company.com/webAppTwo/Site2/Pages/sub/home.aspx
http://mysiteThree.company.com/application/siteThree/SitePages/

I need a function which can retrieve substring from the above URLs. The substring should be upto the 2nd occurence of ‘/’ as follows.

http://mysite.company.com/webApp1/site1/
https://mysite2.company.com/webAppTwo/Site2/
http://mysiteThree.company.com/application/siteThree/

Thanks.

Hi,
for a start try this


var url = document.location.toString();
var chunks = url.split('/');
var newUrl = chunks.slice(0,chunks.length -2).join('/');
alert(newUrl)

Bye

Thanks. I was able to come up with the code that worked. But your code is more elegant and I would use that.

var url_string = String(document.location).toString();
var url_string_array = url_string.split('/',5);
var siteCollName = url_string_array[4];
var siteCollNameLen = siteCollName.length;
var siteNamePos = url_string.indexOf(siteCollName);
var url_desired_string = url_string.substring(0,siteNamePos+siteCollNameLen+1);

alert(url_desired_string);