Just as an alternative, do it without regex (although I assume regex is awfully good here)
var url = "http://test.example.com//mypage/1/test/test//test"; var newurl = url.replace("http://","").split("/"); for(i=0;i<newurl.length;i++) { if(newurl[i] == "") { newurl.splice(i,1); //this for loop takes care of situatiosn where there may be a // or /// instead of a / } } alert(newurl[2]); //returns 1
source share