我想要匹配跨越多行的连续内容,这些行被标记为>。
示例:
Some text <- is not matched
Another Text <- also ignored
> This should be included
> And also this
> This as well
But not this <- ignored匹配只应返回此值(作为完全匹配或单个捕获组):
This should be included
And also this
This as well我目前的方法是这个正则表达式(JavaScript):
/(?:(?<=^> ).+\n)+/gm它确实正确地捕获了我需要的内容,但它没有将三行匹配线组合成一个匹配(因此它生成三个单独的匹配,而不是one)。
为什么不把三组非捕捉组比赛分成一组呢?
是否有可能仅用JavaScript正则表达式来完成此任务?
发布于 2020-04-15 07:08:58
使用您的模式,您将得到3个匹配,而不是1个匹配,因为当移动到下一行时,这个断言(?<=^> )是不正确的,因此引擎将向前移动寻找下一个可能的匹配。
获得结果的一个选项是使用模式首先获取所有匹配,然后对所有>部件执行第二次替换。
^> .+(?:\r?\n>.*)*
const regex = /^> .+(?:\r?\n> .+)*/gm;
const str = `Some text
Another Text
> This should be included
> And also this
> This as well
But not this`;
let result = str.match(regex)[0].replace(/^> /gm, "");
console.log(result);
https://stackoverflow.com/questions/61223031
复制相似问题