JS string to remove consecutive or all repeating characters

Source: Internet
Author: User

JS string to remove consecutive repeating characters

() and \number are used to denote duplicates in the regular number of parentheses, such as: (\d) \1 means repeating the first matching block (\d) is equivalent to if (\d) match to A, the expression is AA

The corresponding can: (some) \1* or (some) \1+ or (some) \1? Represents repeating the first match to get the content any time or at least once or 0 times

1        vars = "1122333455";2 3        varS1 =s;4        varC;5        varCC = S.match (/(\d) \1+/g);//11,22,333,55 of course here () \1* is also possible (because the following is a replacement): 11,22,333,4,556         for(vari = 0;i<cc.length;i++){7 8c = cc[i].substring (0,1);9S1 =S1.replace (cc[i],c);Ten  One        } A  -alert (S1); 12345

JS string to remove all repeating characters and sort the final string

1         vars = "1234321ABACCC";2         varS1 = S.split (""). Sort (). Join ("");3         varCC = S1.match (/(.) \1+/G);//11,22,33,AA,CCC of course here () \1* is also possible (because the following is a replacement): 11,22,33,4,AA,B,CCC4          for(vari = 0;i<cc.length;i++){5 6c = cc[i].substring (0,1);7S1 =S1.replace (cc[i],c);8 9        }Ten  Onealert (S1);//1234ABC

JS string to remove consecutive or all repeating characters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.