regex - Javascript removes letters duplicates and sequence in string -
there many posts , have found few solutions there not perfect. 1 of them:
"aabbhahahahahahahahahahahasetsetset".replace(/[^\w\s]|(.+)\1+/gi, '$1')
the results is:
abhahahahahahaset
i want result:
abhaset
how ?
.+
greedy. takes as can. half of ha
s \1
can match second half. making repetition ungreedy should trick:
/[^\w\s]|(.+?)\1+/gi
by way, i
doesn't change here.
to rid of nested repetitions (e.g. transform aabbaabb
ab
(via aabb
or abab
)) run replacement multiple times until result not change more.
var pattern = /[^\w\s]|(.+?)\1+/g; var output = "aabbaabb"; var input; { input = output; output = input.replace(pattern, "$1"); } while (input != output)
i admit naming of output
bit awkward first repetition, know... 2 difficult problems in computer science cache invalidation, naming things , off-by-one errors.