user7728939
user7728939

Reputation:

Javascript Regex: find & remove letters that repeat LESS THAN a certain number of times

Say, for the string "lllrrrrrrrruuddddr", I need to remove those letters that repeat less than 4 times, ie, "lll" and "uu" and "r", resulting in "rrrrrrrrdddd".

So far I can only use an unweildy workaround by marking those letters repeating at least 4 times with certain special marks and removing all the rest, and then removing my special marks. It's far from elegant, and is prone to error if the original string happens to contain letters idential to my special mark.

Upvotes: 0

Views: 75

Answers (1)

bobble bubble
bobble bubble

Reputation: 18490

I would think of a pattern like this (regex101). With Javascript see this demo (tio.run).

s = s.replace(/(([a-z])\2{3,})|[a-z]/gi,'$1');

Idea is to capture letters, that repeat 4 or more times in group 1 and replace left ones with empty.

Upvotes: 1

Related Questions