try
function getUrl(url) {
var final =
url = url.toLowerCase(); var target = [];
var temp = url.split('/');
if (url.indexOf('.com') > -1) {
var count = temp.indexOf('sites')
count = count + 2;
for (var i = 0; i < count ; i++) {
target.push(temp[i]);
}
final = target.join('/');
}
else {
var count = 2;
if (url.indexOf('//') > -1)
count = count + 2;
for (var i = 0; i < count; i++) {
target.push(temp[i]);
}
final = target.join('/');
}
return final;
}
getUrl('www.yahoo.com/sites/spec/aaa/aaa.aspx')
getUrl('https://facebook.com/sites/test/bbb/test.aspx')
getUrl('uat-abc-link/project/sites/fastplay/kwott/site-page/dbs.html')
getUrl('https://amst-tpage/speg/minaral-water/pages/mpower.asp')
getUrl('www.marklet/mplay/extra.fff/chart.aspx')