Reputation: 4214
I need to replace two or more consecutive characters or character sequences (as in, recurring consecutive patterns of characters) with a single instance of said character or character sequence. These can be any a-z characters. So that;
Foobar
Dummyy
Teststdata
becomes
Fobar
Dumy
Testdata
How do I do this? I'd normally post code on where I have managed to get to on my own, but in this case (and in regex in general), I am hopeless.
Thanks in advance!
Upvotes: 1
Views: 595
Reputation: 214949
/(.+)\1+/g
=> $1
should do the trick
s = "Foobar Dummyy Teststdata"
p = s.replace(/(.+)\1+/g, "$1")
document.write(p)
Do note, however, that this involves lots of backtracking, so use it with care.
Upvotes: 2