-1

I've got a simple string with some of the duplicating characters there. Can someone please help me fix the expression below to remove NOT only duplicated characters but ALL characters that have more than 1 occurance.

console.log('aaabbxxstring'.replace(/(.)(?=.*?\1)/g,'')); // string

I am capturing a matching character using lookahead and replace the match with a whitespace. The question is how to replace the capturing group itself. Or is the entire approakch incorrect?

denims
  • 11
  • 2
  • This seems like a duplicate question. Please take a look at https://stackoverflow.com/questions/35609731/remove-duplicate-in-a-string-javascript – pmpc Jun 28 '23 at 14:53
  • 1
    Does this answer your question? [Remove duplicate in a string - javascript](https://stackoverflow.com/questions/35609731/remove-duplicate-in-a-string-javascript) Instead of splitting on a specific character though you can split with now character to get a list of all characters as substrings. – phuzi Jun 28 '23 at 14:57
  • Do the duplicated characters have to be contiguous to be removed? i.e. with the input "aaba", should the output be "ba", "b", or "aba"? – Daniel Beck Jun 28 '23 at 15:06
  • 1
    @DanielBeck, not necessarily - I am trying to figure out how to completely clear the string from occurances. E.g. if there is 1+ b, then the resulting string should have NO 'b' at all – denims Jun 28 '23 at 15:18
  • The title should probably be something like "Retain characters occurring only once" to make clear what the question is about – JollyJoker Jun 28 '23 at 17:23

4 Answers4

2

Count occurrences using the length of the resulting array when you split the string around the chars.

str.split(c).length

gives you occurrences plus 1.

Convert the string to an array, filter using the occurrences, join to a String.

var str = 'aaabxbxxstring';

const count = (str, c) => str.split(c).length - 1

str = [...str].filter(c => count(str,c) < 2).join('')

console.log(str);

Alternatively call a letter "single" if it occurs only once, meaning splitting around it gives < 3 strings.

var str = 'aaabxbxxstring';

const single = c => str.split(c).length < 3

str = [...str].filter(single).join('')

console.log(str)
JollyJoker
  • 1,256
  • 8
  • 12
0
console.log('aaabbxxstring'.replace(/(.)\1+/g, '')); // string

Explanation:

(.) captures a single character.
\1+ matches one or more occurrences of the captured character.
/g performs a global search to replace all occurrences.
Daniel_Kamel
  • 610
  • 8
  • 29
  • Thanks for the prompt response! This is helpful, but what should the regex look like if I replace 'string' like this: abstringaabbxx Namely the question is to remove ALL occurances from the string. E.g. if there is more than one 'b' the regex should clear them ALL. – denims Jun 28 '23 at 15:07
  • 2
    @denims In that case, don't use regex. – Ted Lyngmo Jun 28 '23 at 15:11
  • @denims the regex should be ```/(.)\1+/g``` – Daniel_Kamel Jun 29 '23 at 07:57
0

I think is the wrong tool for this. You need to first count how many times each character occurs in the string before you can decide what characters to remove.

It is probably easier to get it done using a Map to keep a count of the characters encountered in the string.

Example:

var str = 'aaabxbxxstringaaa';
var map = new Map();

// loop over all the characters and count how many times each character occurs
for(let ch of str) {
  let count = map.get(ch);
  count = count ? count : 0;
  map.set(ch, count + 1);
}

// remove characters occuring more than once:
for(let [ch, count] of map) {
  if(count > 1) {
    str = str.replaceAll(ch, '');
  }
}

console.log(str);

Disclaimer: I'm not used to javascript so there may be more idiomatic ways of doing this .

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
0

RegEx is not the right tool for this, but here is a thought experiment come to life anyway :)

const s = 'aaabbxxstringnnnhello'
const pattern = /(.)(?!.*\1)/g
console.log(s.match(pattern).join('')); // abxstrignhelo
Tony B
  • 159
  • 6