Emphram Stavanger
Emphram Stavanger

Reputation: 4214

Regex: two or more consecutive characters or character sequences

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

Answers (1)

georg
georg

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

Related Questions