gpt4 book ai didi

javascript - 如何将文本正文拆分为句子和/或段落分隔符?

转载 作者:行者123 更新时间:2023-12-03 12:21:15 24 4
gpt4 key购买 nike

我想分割文本正文,例如:

var str = "This is one.  Two because of space break
This is number three!


And Four?!?!"

使用 here 中的 str.match(/[^\.!\?]+[\.!\?]+/g )我得到以下 3。

[ 'This is one.',
' Two because of space break\r\n This is number three!',
' \r\n\r\n\r\n And Four?!?!' ]

相反,由于分页符,我想要 4 个不同且干净的(无\r\n)值。我尝试在 match 函数之前使用 str.replace(/\r?\n/g,'.'); ,但我想知道是否有更干净的方法组合正则表达式?

我想得到:

['This is one.', 'Two because of space break', 'This is number three!', 'And Four?!?!']

最佳答案

这是你想要的吗?

str.match(/[^\s.!?]+[^.!?\r\n]+[.!?]*/g);

关于javascript - 如何将文本正文拆分为句子和/或段落分隔符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24441291/

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