Reputation: 20953
I want to use String.prototype.split
to split the string if the previous character is not equal to the next character, here's the target result:
'abcd' => ['a', 'b', 'c', 'd']
'aaabbbccd' => ['aaa', 'bbb', 'cc', 'd']
I know it's possible to split a string by only lookbacks
:
const result = 'aaabbbccd'.split(/(?<=a)/);
console.log(result); // ["a", "a", "a", "bbbccd"]
So I wanted to find an expression to find the delimiter of two characters that its lookback is not equal to lookahead.
But I tried this, and it doesn't work:
const result = 'aaabbcccd'.split(/(?<!\2)(?=.)/);
console.log(result); // ['a', 'a', 'a', 'b', 'b', 'c', 'c', 'c', 'd']
So is there a regular expression to achieve this? Or it's just simply impossible to do that with regex?
Upvotes: 1
Views: 83
Reputation: 50974
Using backreferences, you can use .match()
instead of .split()
to get your desired result like so:
const r1 = 'abcd'.match(/(.)\1*/g);
const r2 = 'aaabbcccd'.match(/(.)\1*/g);
console.log(r1); // ['a', 'b', 'c', 'd']
console.log(r2); // ['aaa', 'bb', 'ccc', 'd']
This will match any character (.)
, followed by the same character \1
zero or more times
Upvotes: 5