如何将一段以,分割的数字文字串排序并去除相同的数字!

例如“1,2,5,2,4,3,6,2,21”最后得到“1,2,3,4,5,6,21”
---------------------------------------------------------------

针对用单位数及多位数混杂的排序问题的解决:

1<script>   
2function s1(a,b){return a-b}   
3var str = "1,2,5,21,4,3,6,2,21,2,323";   
4str = (","+ str).replace(/(.\d+)(.*)(\1)/g, "$2");   
5str = str.substr(1).split(",").sort(s1).join(",");   
6alert(str);   
7</script>

---------------------------------------------------------------

1<script>   
2var str="112333"   
3var reg = /(.)(.*)(\1)/gi   
4while(str.match(reg))str=str.replace(reg,"$1$2")   
5alert(str);   
6</script>

---------------------------------------------------------------

1<script>   
2var str = "1,2,5,2,4,3,6,2";   
3str = str.split(",").sort().join(",") + ",";   
4str = str.replace(/(\w+,)(\1)+/,"$1")   
5str = str.substr(0,str.length-1);   
6alert(str);   
7</script>
Published At
Categories with Web编程
comments powered by Disqus