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.