简体   繁体   English

使用 javascript 使用正则表达式环视拆分字符串

[英]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:如果前一个字符不等于下一个字符,我想使用String.prototype.split拆分字符串,这是目标结果:

'abcd' => ['a', 'b', 'c', 'd'] 'abcd' => ['a', 'b', 'c', 'd']

'aaabbbccd' => ['aaa', 'bbb', 'cc', 'd'] 'aaabbbccd' => ['aaa', 'bbb', 'cc', 'd']

I know it's possible to split a string by only lookbacks :我知道可以仅通过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:使用反向引用,您可以使用.match()而不是.split()来获得所需的结果,如下所示:

 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这将匹配任何字符(.) ,后跟相同的字符\\1零次或多次

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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