zoukankan      html  css  js  c++  java
  • JS去除重复字符串

    去除重复字符串我用到的三种方法:

    把例子贴上,用jquery方便些,首先要搭好环境,就是在同一目录下(同一文件夹下)保证有所使用的jquery1.8.1(如果是其他版本就在html代码中作相应改动)

    第一、

    [html] view plaincopy
     
    1. <html>  
    2. <head>  
    3. <script src="jquery-1.8.1.js"></script>  
    4.   
    5. <SCRIPT LANGUAGE="JavaScript">  
    6.   
    7. $(function(){  
    8.     $('#delRepeat').click(function(){  
    9.   
    10.   
    11.         var str = $('#repeatValue').val();  
    12.             var strArr=str.split("");//把字符串分割成一个数组  
    13.               
    14.             strArr.sort();//排序  
    15.             var result=new Array();//创建出一个结果数组  
    16.             var tempStr="";  
    17.             for(var i in strArr)  
    18.             {  
    19.                  if(strArr[i] != tempStr)  
    20.                  {  
    21.                       result.push(strArr[i]);  
    22.                       tempStr=strArr[i];  
    23.                  }  
    24.                  else  
    25.                  {  
    26.                       continue;  
    27.                  }  
    28.             }  
    29.             $('#noRepeat').val(result.join(""))//把数组连成字符串并展示到页面  
    30.     })  
    31. })  
    32.   
    33.   
    34. </SCRIPT>  
    35. </head>  
    36. <body>  
    37. 原值<input id="repeatValue" type="text" ><input id="delRepeat" type="button" value="去重">  
    38. <input type="text" id="noRepeat">  
    39. </body>  
    40. </html>  

    说明:通常就是把字符串分割成数组,再对数组操作,相对来说数组的方法多些,方便些,最后再join成字符串

    关于sort()方法,之所以先对数组元素排序,就是因为可以把相同的字符归到一起,就不用再双层循环,要不然就得拿到每个元素,和剩余的逐个比对,这个sort会按照ASCII 字符顺序进行升序排列

    第二、

    [html] view plaincopy
     
    1. <html>  
    2. <head>  
    3. <script src="jquery-1.8.1.js"></script>  
    4.   
    5. <SCRIPT LANGUAGE="JavaScript">  
    6.   
    7. $(function(){  
    8.     $('#delRepeat').click(function(){  
    9.         var str = $('#repeatValue').val();  
    10.             var strArr=str.split("");  
    11.             //排序  
    12.             strArr.sort();  
    13.                 var result =$.unique(strArr);  
    14.             $('#noRepeat').val(result.join(""));  
    15.                  
    16.     })  
    17. })  
    18.   
    19.   
    20. </SCRIPT>  
    21. </head>  
    22. <body>  
    23. 原值<input id="repeatValue" type="text" ><input id="delRepeat" type="button" value="去重">  
    24. <input type="text" id="noRepeat">  
    25. </body>  
    26. </html>  

    说明,这个unique方法确实方便,但有两个缺陷:

    1、只对数组有效(直接字符串不行),并且该数组不能是数字数组,

    2、只对相邻的重复元素有效,隔开的不行。

    例如:[a,a,b,b,c,c]---unique----[a,b,c]有效

    [a,a,b,b,c,c,a]--unique-->[a,b,c,a]元素a仍然重复,无效

    因此调用unique之前必须调用一下sort方法将其重复元素重排一下,挨在一起

    但也因为调用了sort方法,顺序给重排了如:[b,b,c,c,a,a]---unique-->[a,b,c]不是[b,c,a]

    第三、

    [html] view plaincopy
     
    1. <html>  
    2. <head>  
    3. <script src="jquery-1.8.1.js"></script>  
    4.   
    5. <SCRIPT LANGUAGE="JavaScript">  
    6.   
    7. $(function(){  
    8.     $('#delRepeat').click(function(){  
    9.         var str = $('#repeatValue').val();  
    10.                     var reg = /(.)(?=.*1)/g;//预搜索方式(有的叫断言)       
    11.                     var result = str.replace(reg, "");   
    12.                     $('#noRepeat').val(result);  
    13.     })  
    14. })  
    15. </SCRIPT>  
    16. </head>  
    17. <body>  
    18. 原值<input id="repeatValue" type="text" value="aca" ><input id="delRepeat" type="button" value="去重">  
    19. <input type="text" id="noRepeat">  
    20. </body>  
    21. </html>  

    说明:

    var reg =/(.)(?=.*1)/g;

    .匹配任意字符,但只能匹配任意字符中的一个

    (.)加上()就是将匹配的该字符存储起来供以后引用

    (?=)预搜索(也有叫断言的,也有叫预查的),指明某个字符的右侧是什么,但不包含这部分,只取这个‘某个字符’

    如:p(?=ing)     匹配字符串ping匹配成功,但匹配到的字符是p不是ping

    (?=.*1) 这个1就是指的前面(.)的这个字符,之前说它被加上小括号就是被存储起来了,现在1就是取存储的第一个(共一个)

    *匹配次数,也有人称之为量词,指出现任意次

    .*指出现任意次任意字符

    (.)(?=.*1)指第一个匹配字符,如果右侧出现的内容中包含该字符时就匹配上该字符

     g    globle,全局匹配模式,匹配所有字符串

    这个去重的结果其实是倒着来排序的,就是说重复字符出现在前面的都被置空了,是按一个字符从后往前出现的顺序排的

  • 相关阅读:
    java知识点-高级
    Java中高级面试题
    项目基础
    TFS Build Error: CSC : fatal error CS0042: Unexpected error creating debug information file 'xxxx.PDB'
    System.FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list
    (C#) System.BadImageFormatException: An attempt was made to load a program with an incorrect format.
    (C#) 引用工程中发现有黄色叹号
    (C#).NET 2.0 ~ 4.0 OS requirements.
    (WCF) WCF and Service Debug
    (WCF) WCF Service Hosting.
  • 原文地址:https://www.cnblogs.com/babysay123/p/4798736.html
Copyright © 2011-2022 走看看