我有这样一段文字:

const text = "If you look at a map of Europe... you will... notice. That apart from the big... landmass known as the continent, there are two small islands to the west."

我需要将其分割为一个点(并将该点保留在句子中),但我只需要将其分割为一个点,因此我需要以下数组:

const result = [
 "If you look at a map of Europe... you will... notice.",
 "That apart from the big... landmass known as the continent, there are two small islands to the west."
]

我已经创建了一个可以用圆点、问号和感叹号分隔句子的函数,但当句子中有三个圆点时,它不能正常工作.

function splitByPunctuationMark(str) {
 return str.split(/(?<=[!.?])/).map(value => value.trim())
}

UPDATED

splitByPunctuationMark()会给出以下结果:

#1当源文本中没有三个点时

const result = splitByPunctuationMark("If you look at a map of Europe you will notice. That apart from the big landmass known as the continent, there are two small islands to the west.")

console.log(result)
/*
[
 "If you look at a map of Europe you will notice.", 
 "That apart from the big landmass known as the continent, there are two small islands to the west."
]
*/

#2当原文中有三个点时

const result = splitByPunctuationMark("If you look at a map of Europe... you will... notice. That apart from the big... landmass known as the continent, there are two small islands to the west.")

console.log(result)
/*
[
 "If you look at a map of Europe.", 
 ".", 
 ".", 
 "you will.", 
 ".", 
 ".", 
 "notice.", 
 "That apart from the big.", 
 ".", 
 ".", 
 "landmass known as the continent, there are two small islands to the west."
]
*/

推荐答案

这里有一种方法.起初我把所有的"."通过将它们转换为分隔符字符串来消失.需要仔细 Select 此字符串,以便在目标字符串中找不到它.在剩下的单曲上分裂后".然后我将"..."回到原来的位置.

const text = "If you look at a map of Europe... you will... notice. That apart from the big... landmass known as the continent, there are two small islands to the west.And here is a third ... is it a sentence?   And a forth!"


const sep="@threedots@",res=text.replaceAll("...",sep).split(/(?<=[.!?])\s*/).map(e=>e.replaceAll(sep,"..."));

console.log(res);

为了保存"."在每句话的末尾,我在正则表达式中使用了一个后视:/(?<=[.!?])\s*/.这将把0...n空格字符视为分隔符模式,如果它们紧跟在"."、"!"之后.或者在"?"之后.

@Martin Niederl非常正确地 comments 了重复出现any number个点的可能性.他提出了一个我也认为有帮助的解决方案.以下是我对它的看法(也考虑到其他句尾字符):

const text = "If you look at a map of Europe..... you will... notice. That apart from the big.. landmass known as the continent, there are two small islands to the west.And here is a third .... Is it a sentence?   And a forth!"
const res=text.split(/(?<=(?<!\.)[.!?](?!\.))\s*/);
console.log(res);

现在,我有了一个正后视,它包含一个模式,该模式由"."的负后视组成,后面紧跟一个字符".","!"或者"?"又一次负面展望,又一次"."在正向回溯之后,我立即要求一个从0到任意数量的空格字符的序列.

Javascript相关问答推荐

当promise 在拒绝处理程序被锁定之前被拒绝时,为什么我们会得到未捕获的错误?

如何在表格上拥有水平滚动条,在正文页面上拥有垂直滚动条,同时还对html表格的标题使用位置粘性?

我可以使用CSS有效地实现最大宽度=100%和最大高度=100%,而无需父母有明确的/固定的宽度和高度,替代方法吗?

django无法解析余数:[0] from carray[0]'

未捕获错误:在注销后重定向到/login页面时找不到匹配的路由

在Vite React库中添加子模块路径

MathJax可以导入本地HTML文档使用的JS文件吗?

构造HTML表单以使用表单数据创建对象数组

从页面到应用程序(NextJS):REST.STATUS不是一个函数

以编程方式聚焦的链接将被聚焦,但样式不适用

与svg相反;S getPointAtLength(D)-我想要getLengthAtPoint(x,y)

如何在Java脚本中对列表中的特定元素进行排序?

如何访问此数组中的值?

FireBase云函数-函数外部的ENV变量

JavaScript&;Reaction-如何避免在不使用字典/对象的情况下出现地狱?

无法使用npm install安装react-dom、react和next

如何在Firebase中读取对象的特定字段?

我在JS代码中收到超过最大调用堆栈大小的错误,但我找不到原因.有谁能帮我搬一下吗?

webpack 5和mini-css-extract-plugin在将scss保存到css文件后不加载css

使用JAVASCRIPT的偏移力矩函数库问题