JS Remove Duplicate string

Source: Internet
Author: User

From: http://blog.sina.com.cn/s/blog_65c2ec5e0101j133.html

Method One:

It is usually to divide the string into arrays and then manipulate the array.

varstr = "AABDEEGDCFFDSF", result=[], TempStr= ""; varRemoveduplicate =function(str) {vararr = Str.split (");//split a string into an array        //Arr.sort ();//Sort         for(vari = 0; i < arr.length; i++){            if(Arr[i]!==tempstr)                {Result.push (arr[i]); TempStr=Arr[i]; }Else{                Continue; }        }        returnResult.join (""); } console.log (Removeduplicate (str)); //ABDEGDCFDSF

Method Two:

var function (str) {var reg =/(.) (? =.*\1)/g; var result = Str.replace (Reg, ""); return  //ABDEGDCFDSF

Description

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

2. Match any character, but only one of any characters;

3, (.) Plus () is to store the matched character for later reference;

4, (=) Pre-search (also called assertion, also called pre-check), indicates what the right side of a character, but does not contain this part, only take this ' one character ', such as: P (? =ing) matches the string ping when the match succeeds, but the match to the character is P is not ping;

5, (=.*\1) This \1 refers to the front (.) This character, before saying that it was added with parentheses is stored, now \1 is the first to take the storage (a total of):

* Number of matches, also known as quantifiers, refers to the occurrence of any time

. * refers to any character appearing any time

6, (.) (? =.*\1) refers to the first matching character, and matches the character if it appears on the right;

7, G refers to globle, global matching mode, matching all strings;

8, the result of this deduplication is in fact inverted to sort, that is, repeated characters appear in front of all are set empty, is a character from the back and forth in the order of appearance.

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.