gpt4 book ai didi

javascript - 如何只用一个点而不是三个点来分割文本?

转载 作者:行者123 更新时间:2023-12-01 22:48:01 24 4
gpt4 key购买 nike

我有这段文字:

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())
}

已更新

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."
]
*/

最佳答案

这是一种方法。起初我通过将它们转换为分隔符字符串来使所有“...”消失。这个字符串需要仔细选择,所以它不会在目标字符串的任何地方被发现。在剩余的单个“.”处拆分后。然后我将“...”替换回原来的位置。

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 非常正确地评论了任何数量 重复点出现的可能性。他提出了一个我也认为有帮助的解决方案。这是我的看法(也允许其他句末字符):

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 - 如何只用一个点而不是三个点来分割文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74931204/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com