Return a string with no instances of 3 identical consecutive letters


[Exercise removed due to DMCA copyright claim]

can anyone point me in the right direction ?

thx in advance

The first thing I do with such challenges is write down how I do it in my head.

have you tried using splice?

OK, i found a way :

function cleanString(S){
  for(var i = 0; i < S.length; i++) {
    let re = new RegExp(`${S[i]}{3,}`,"g");
      S = S.replace(re, S[i] + S[i]);
  return S;

You need to use back-references. For example if you capture something with () you can refer back to that same match with \1. If you capture two things, then the first is \1 and the 2nd is \2 and so on. Here’s an example of what I mean:

(\w)\1{2,} -- Flags: g

Test it here: against your two examples or type whatever you feel would serve as a good test input.

And my solution if you don’t really get to solve it with RegExp in one sweep, read ir only if you’re stuck:


function solution(str) {
  const pattern = /(\w)\1{2,}/g

  return str.replace(pattern, function(_, letter) {
    return letter + letter
1 Like