Regex remove all duplicate characters

I am looking for a regex that will remove all characters that have been repeated in a string. I have already solved this with a loop. Just wondering if there is a regex that can do the same.

this is what i have so far:

function onlyUnique(str) { var re = /(.)(?=.*\1)/g return str.replace(re, ''); } 

This line:

 "rc iauauc!gcusa_usdiscgaesracg" 

should end as follows:

 " !_de" 
+5
source share
5 answers

You can use Array#filter with Array#indexOf and Array#lastIndexOf to check if this element repeats.

 var str = "rc iauauc!gcusa_usdiscgaesracg"; // Split to get array var arr = str.split(''); // Filter splitted array str = arr.filter(function (e) { // If index and lastIndex are equal, the element is not repeated return arr.indexOf(e) === arr.lastIndexOf(e); }).join(''); // Join to get string from array console.log(str); document.write(str); 
+3
source

Your regular expression searches for pairs of duplicate characters and only deletes the first. Therefore, the last duplicate will not be deleted.

To solve this problem, you should remove all duplicates at the same time, but I don’t think you can do it with one replace .

Instead, I would build a map that counts the occurrences of each character, and then iterates over the line again, clicking on the characters that appear only once for a new line:

 function onlyUnique(str) { var map = Object.create(null); for(var i=0; i<str.length; ++i) map[str[i]] = (map[str[i]] || 0) + 1; var chars = []; for(var i=0; i<str.length; ++i) if(map[str[i]] === 1) chars.push(str[i]); return chars.join(''); } 

Unlike indexOf , hash map searches are on average constant. So the call cost with character string n will be n .

+1
source

Well, I don’t know if regex can do this, but you can work with it for a loop, for example:

 function unikChars(str) { store = []; for (var a = 0, len = str.length; a < len; a++) { var ch = str.charAt(a); if (str.indexOf(ch) == a && str.indexOf(ch, a + 1) == -1) { store.push(ch); } } return store.join(""); } var str = 'rc iauauc!gcusa_usdiscgaesracg'; console.log(unikChars(str)); //gives !_de 

Demo :: jsFiddle

+1
source

If you want to do this with a regular expression, you can use your own regular expression with a callback function inside the replacement.

 var re = /(.)(?=.*\1)/g; var str = 'rc iauauc!gcusa_usdiscgaesracg'; var result = str; str.replace(re, function(m, g1) { result = result.replace(RegExp(g1.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "g"), ''); }); document.getElementById("r").innerHTML = "'" + result + "'"; 
 <div id="r"/> 

Idea: get a duplicate character and remove it from the input string. Note that escaping is necessary if the character can be a special regex metacharacter (thus using g1.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") ).

Another idea belongs to Washington Guedes in the remote answer , I will just add my own implementation here (with removing duplicate characters from the character class and escaping special regular expression characters):

 var s = "rc iauauc!gcusa_u]sdiscgaesracg]"; var delimiters= '[' + s.match(/(.)(?=.*\1)/g).filter(function(value, index, self) { // find all repeating chars return self.indexOf(value) === index; // get unique values only }).join('').replace(/[.*+?^${}()|[\]\\]/g, "\\$&") + ']'; // escape special chars var regex = new RegExp(delimiters, 'g'); // build the global regex from the delimiters var result = s.replace(regex, ''); // obtain the result document.getElementById("r2").innerHTML = "'" + result + "'"; 
 <div id="r2"/> 

NOTE If you want to also support newline characters, replace . on [^] or [\s\S] inside the regular expression pattern.

+1
source
 function onlyUnique(str) { // match the characters you want to remove var match = str.match(/(.)(?=.*\1)/g); if (match) { // build your regex pattern match = '[' + match.join('') + ']'; } // if string is already unique return the string else { return str } // create a regex with the characters you want to remove var re = new RegExp(match, 'g'); return str.replace(re, ''); } 
0
source

Source: https://habr.com/ru/post/1239686/


All Articles