我正在尝试使用 javascript 的拆分从字符串中获取句子,但保留分隔符,例如 !?。
到目前为止我有
sentences = text.split(/[\\.!?]/);
哪个有效,但不包括每个句子的结尾标点符号 (.!?)。
有谁知道这样做的方法吗?
原文由 daktau 发布,翻译遵循 CC BY-SA 4.0 许可协议
我正在尝试使用 javascript 的拆分从字符串中获取句子,但保留分隔符,例如 !?。
到目前为止我有
sentences = text.split(/[\\.!?]/);
哪个有效,但不包括每个句子的结尾标点符号 (.!?)。
有谁知道这样做的方法吗?
原文由 daktau 发布,翻译遵循 CC BY-SA 4.0 许可协议
以下是对 Larry 的回答的一个小补充,它也将匹配附加句子:
text.match(/\(?[^\.\?\!]+[\.!\?]\)?/g);
应用于:
text = "If he's restin', I'll wake him up! (Shouts at the cage.)
'Ello, Mister Polly Parrot! (Owner hits the cage.) There, he moved!!!"
给出:
["If he's restin', I'll wake him up!", " (Shouts at the cage.)",
" 'Ello, Mister Polly Parrot!", " (Owner hits the cage.)", " There, he moved!!!"]
原文由 mircealungu 发布,翻译遵循 CC BY-SA 3.0 许可协议
13 回答12.8k 阅读
7 回答1.9k 阅读
3 回答1.1k 阅读✓ 已解决
2 回答1.1k 阅读✓ 已解决
6 回答818 阅读✓ 已解决
6 回答1k 阅读
2 回答1.3k 阅读✓ 已解决
您需要使用匹配而不是拆分。
尝试这个。