Search code examples
javascriptregexregex-lookarounds

Split a string with regular expression lookarounds using javascript


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?


Solution

  • 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