简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM