首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在javascript中将字符串拆分成句子

在javascript中将字符串拆分成句子
EN

Stack Overflow用户
提问于 2013-09-20 18:34:16
回答 5查看 17.9K关注 0票数 22

目前,我正在开发一个将长列拆分成短列的应用程序。为此,我将整个文本拆分为单词,但目前我的正则表达式也拆分了数字。

我所做的是:

代码语言:javascript
复制
str = "This is a long string with some numbers [125.000,55 and 140.000] and an end. This is another sentence.";
sentences = str.replace(/\.+/g,'.|').replace(/\?/g,'?|').replace(/\!/g,'!|').split("|");

结果是:

代码语言:javascript
复制
Array [
    "This is a long string with some numbers [125.",
    "000,55 and 140.",
    "000] and an end.",
    " This is another sentence."
]

期望的结果将是:

代码语言:javascript
复制
Array [
    "This is a long string with some numbers [125.000, 140.000] and an end.",
    "This is another sentence"
]

如何更改我的正则表达式才能实现这一点?我需要注意一些我可能会遇到的问题吗?或者搜索". ""? ""! "就足够了吗?

EN

回答 5

Stack Overflow用户

发布于 2013-09-20 18:54:54

您可以利用下一句话以大写字母或数字开头的特点。

代码语言:javascript
复制
.*?(?:\.|!|\?)(?:(?= [A-Z0-9])|$)

Debuggex Demo

它将此文本拆分

代码语言:javascript
复制
This is a long string with some numbers [125.000,55 and 140.000] and an end. This is another sentence. Sencenes beginning with numbers work. 10 people like that.

到句子中去:

代码语言:javascript
复制
This is a long string with some numbers [125.000,55 and 140.000] and an end.
This is another sentence.
Sencenes beginning with numbers work.
10 people like that.

jsfiddle

票数 6
EN

Stack Overflow用户

发布于 2013-09-20 18:41:55

您可以更安全地使用先行查找,以确保点后面的内容不是数字。

代码语言:javascript
复制
var str ="This is a long string with some numbers [125.000,55 and 140.000] and an end. This is another sentence."

var sentences = str.replace(/\.(?!\d)/g,'.|');
console.log(sentences);

如果你想更安全,你可以检查后面的是不是数字,但由于JS不支持后视,你需要捕获前一个字符并在替换字符串中使用它。

代码语言:javascript
复制
var str ="This is another sentence.1 is a good number"

var sentences = str.replace(/\.(?!\d)|([^\d])\.(?=\d)/g,'$1.|');
console.log(sentences);

一个更简单的解决方案是转义数字中的点(例如,用$$$$替换它们),进行拆分,然后取消转义点。

票数 4
EN

Stack Overflow用户

发布于 2013-09-20 18:55:31

您忘记在regexp中输入'\s‘。

试试这个

代码语言:javascript
复制
var str = "This is a long string with some numbers [125.000,55 and 140.000] and an end. This is another sentence.";
var sentences = str.replace(/\.\s+/g,'.|').replace(/\?\s/g,'?|').replace(/\!\s/g,'!|').split("|");
console.log(sentences[0]);
console.log(sentences[1]);

http://jsfiddle.net/hrRrW/

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18914629

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档