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

标签 javascript jquery regex

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

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/

相关文章:

javascript - dist/文件夹中的JS文件和根目录中的JS文件有什么区别?

javascript - Edge、Internet Explorer 中新 WebSocket 出现语法错误

javascript - 检测长按 caph-list 项目上的 Enter/OK 键

jquery - 如何使div向上滚动并固定在顶部

javascript - RegExp 构造函数和 Regex 文字测试函数之间的区别?

r - R 中是否可以为正则表达式子字符串提供一组选项?

ruby - 去除/替换字符串中的空格

javascript - Select2 仅更新禁用选项一次

javascript - 回发后在 TextBox 中设置焦点

jquery - jquery中的循环列表