我有这样一段文字:
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."
]
*/