getPageNumRange
<script> function getPageNumRange(pagenumstr) { var pages=pagenumstr.split(";"); pages.sort(function(a,b){ return a-b; }); var delta=0; var lianxiu=new Array(); var bulianxiu=new Array(); for(var i=0;i<pages.length;i++){ delta=0; delta=(1*pages[i+1])-(1*pages[i]); if(1==delta) { //连续 lianxiu.push(pages[i]); lianxiu.push(pages[i+1]); } else { if(delta>1) { //不连续 lianxiu.push("|"); if(lianxiu.indexOf(pages[i])==-1) bulianxiu.push(pages[i]); } } if (i==pages.length-1){ if(lianxiu.indexOf(pages[i])==-1) { bulianxiu.push(pages[i]); } } } lianxiuStr=lianxiu.toString().replace(/\|\,/g,"|").replace(/\,\|/g,"|"); var lianxiuArr=lianxiuStr.split("|"); var ResultStr1=""; for(var i=0;i<lianxiuArr.length;i++){ var yema=lianxiuArr[i]; if(""!=yema) { var yemaArr=yema.split(","); if(""==ResultStr1) { ResultStr1=yemaArr[0]+"-"+yemaArr[yemaArr.length-1]; } else { ResultStr1=ResultStr1+";"+yemaArr[0]+"-"+yemaArr[yemaArr.length-1]; } } }//for var resultPageNums=""; var resultList=ResultStr1.split(";"); for(var i=0;i<bulianxiu.length;i++){ resultList.push(bulianxiu[i]); } resultList.sort(function(a,b){ var v1,v2; if(a.indexOf("-")!=-1) { v1=a.substr(0,a.indexOf("-"))*1; }else v1=a*1; if(b.indexOf("-")!=-1) { v2=b.substr(0,b.indexOf("-"))*1; }else v2=b*1; return v1-v2; }); return resultList.toString(); } alert(getPageNumRange("9;10;17;18;19;21;23;25;26;27;28;30;36;37;38;40")); </script>