如何将一段文本分割成句子和/或段落

How can I split a body of text into both sentences and/or paragraph breaks?

本文关键字:句子 分割 段落 文本 一段      更新时间:2023-09-26

我想分割文本主体,如:

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

And Four?!?!"

从这里使用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)值,因为分页符。我试着在匹配函数之前使用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);