Remove consecutive duplicate characters in a string javascript

前端 未结 4 1108
臣服心动
臣服心动 2020-12-20 22:14

I have some string like 11122_11255_12_223_12 and the output I wish to have is this: 12_125_12_23_12
I already looked at this and also this a

4条回答
  •  温柔的废话
    2020-12-20 22:45

    You can use capture group and back-reference:

    result = str.replace(/(.)\1+/g, '$1')
    

    RegEx Demo

    • (.): Match any character and capture in group #1
    • \1+: Match 1+ characters same as in capture group #1

提交回复
热议问题