我正试图从一段文本中打破句子,我目前的解决方案是有效的,但它并不总是有效的。这是我的正则表达式
text.replaceAll(/[^Mr|^mr|^Mrs|^mrs|^Ms|^ms](.|?|!)s[A-Z]/g, r => r.replace(/s/, "{break}")).split("{break}");
的方式应该是找到工作一段时间然后空间后跟大写字母除先生,先生,夫人,夫人,女士,女士或目前,除了这句话结束时在一个年代,m,或r。我知道这是因为[]匹配任何字符,我的问题是我怎么写这正是如此我想(匹配完整的单词,而不是单个字符)
一个失败的字符串示例是
"A string with words. A new string."
和一个传递
"A string. A new string."
如何在不打断名称标题的情况下分割句子
我喜欢正则表达式,你想把你的结果分组,只抓住句号。然后,将我们用作标识符组的内容替换为$1变量,并使用{break}条目。
let text = "A string with words. Mr. Andrews wrote a new string. It went something like Mrs. Doubtfire's best line. But what if 3.1 people want to sign up for Gecko? What if it ends in a question mark?";
const OPExample = text.replace(/(?<!Mr|Mrs|Ms|Dr|Sr)([.???!?]) ([A-Z])/gi, "$1{break}$2")
const SplitLines = OPExample.split("{break}");
console.log(OPExample); // "A string with words.{break}Mr. Andrews wrote a new string.{break}It went something like Mrs. Doubtfire's best line.{break}But what if 3.1 people want to sign up for Gheko?{break}What if it ends in a question mark?"
console.log(SplitLines); // ["A string with words.","Mr. Andrews wrote a new string.","It went something like Mrs. Doubtfire's best line.","But what if 3.1 people want to sign up for Gheko?","What if it ends in a question mark?"]
Codepen