A simple replace regular expression to remove adjacent duplicated characters from a given string in js
string.replace(/(.)(?=\\1)/g, '')
For example
var string = 'aaaaaaaaaabccccccccccccccccccccdedfgh ijjjjjjjjjjkkkkkkll'var processed = string.replace(/(.)(?=\1)/g, '')// processed is 'abcdedfgh ijkl'
Explanation:
- (.) : match one single character and capture it to
group 1
- (?=\1) : positive look ahead, to match the results of
group 1
above, and not including thegroup 1
in the results - /g : interate through the whole string