I have this regex that works perfectly with PHP:
$que = preg_replace("/(?<=^| )([a-z])\\1*(?= |$)\K|([a-z])(?=\\2)/","$3",$que);
This regex removes repeated chars inside strings (for example, axxd becomes axd however xxx will still become xxx). The problem that I am facing, is because it does not work with JS (I think the negative lookbehind does not work with JS).
More examples:
- the string
aaa baaab xxxwould becomeaaa bab xxx - the string
ahhj aaab cc iiikwould becomeahj ab cc ik
Do you have a solution for this that is at least a little efficient? I mean, I will probably use this regex on a string with 1k chars, so if the regex is not efficient, the browser may freeze.
The negative lookbehind is not likely to be your issue as they are supported on almost all current release browsers. However JavaScript regex doesn't recognise
\Kas a meta sequence but rather as a literalK. You can work around that using this regex:This matches either
\b([a-z])\1+(?!\1|\b):\b: word boundary([a-z]): a letter, captured in group 1\1+: one or more repetitions of the captured letter(?!\1|\b): lookahead assertion that the next location is not another repetition of the captured letter or a word boundaryor
(?<=([a-z]))((?!\2)[a-z])\3+:(?<=([a-z])): a positive lookbehind for a letter, captured in group 2((?!\2)[a-z]): another letter which is not the same as the previously captured letter, captured in group 3\3+: one of more repetitions of the captured letterThe first part of the regex will capture repeated letters at the beginning of a word; the second part captures repeated letters in the middle or at the end of a word.
You can then replace with
$1$3which will replace any repeated letter matched by the regex with just a single copy of itself.Regex demo on regex101
In JavaScript:
PHP demo on 3v4l.org