gpt4 book ai didi

javascript - 如何获取以换行符结尾或以 javascript 正则表达式中的字符串末尾结尾的字符串列表?

转载 作者:行者123 更新时间:2023-11-28 12:26:19 25 4
gpt4 key购买 nike

我现在对正则表达式感到非常沮丧。鉴于:

var text = "This is a sentence.\nThis is another sentence\n\nThis is the last sentence!"

我希望正则表达式返回给我:

{"This is a sentence.\n", "This is another sentence\n\n", "This is the last sentence!"}

我想我应该使用

var matches = text.match(/.+[\n+\Z]/) 

但是\Z 似乎不起作用。 JavaScript 有字符串结尾匹配器吗?

最佳答案

您可以使用以下正则表达式。

var matches = text.match(/.+\n*/g);

Working Demo

或者您可以匹配换行序列“一次或多次”或字符串末尾。

var matches = text.match(/.+(?:\n+|$)/g);

关于javascript - 如何获取以换行符结尾或以 javascript 正则表达式中的字符串末尾结尾的字符串列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28121766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com