admin管理员组文章数量:1335138
I want to receive a lot of text (e.g. a book chapter), and create an array of the sentences.
My current code is:
text.match( /[^\.!\?]+[\.!\?]+["']?/g );
This only works when the text ends with one of [. ! ?]. If the final sentence has no punctuation at the end, it's lost.
How do I split my text into sentences, allowing for the final sentence to not have punctuation?
I want to receive a lot of text (e.g. a book chapter), and create an array of the sentences.
My current code is:
text.match( /[^\.!\?]+[\.!\?]+["']?/g );
This only works when the text ends with one of [. ! ?]. If the final sentence has no punctuation at the end, it's lost.
How do I split my text into sentences, allowing for the final sentence to not have punctuation?
Share Improve this question asked Dec 4, 2016 at 11:28 Mirror318Mirror318 12.7k14 gold badges70 silver badges115 bronze badges 7- Does the final sentence have a line break? – jstice4all Commented Dec 4, 2016 at 11:30
-
add
\n
i.e new line – SaidbakR Commented Dec 4, 2016 at 11:30 - \n works only if there are no other line breaks in the text, which sounds unlikely. – JJJ Commented Dec 4, 2016 at 11:32
- You may include an example, you'll get more relevant answers – Thomas Ayoub Commented Dec 4, 2016 at 11:35
- What about if you have abbreviations in your sentences? – flec Commented Dec 4, 2016 at 11:37
4 Answers
Reset to default 4Use $
to match the end of the string:
/[^\.!\?]+[\.!\?]+["']?|.+$/g
Or maybe you want to allow whitespace characters at the end:
/[^\.!\?]+[\.!\?]+["']?|\s*$/g
It depends on the characters in the text but
text.match( /[^\.!\?]+[\.!\?]+|[^\.!\?]+/g );
can do the job.
(If it doesn't work could you provide a few sentences what you can't match?)
Depending on whether you need the punctuation of your sentences in your result you can just use "split"
var txt="One potato. Two Potato. Three";
txt.split( /[\.!\?]+/ );
[ 'One potato', ' Two Potato', ' Three' ]
You can just use [^\.!\?]+
, you don't need the rest:
text = "Mr. Brown Fox. hello world. hi again! hello one more time"
console.log(text.match(/[^\.!\?]+/g))
本文标签: javascriptJS split text into sentencesStack Overflow
版权声明:本文标题:javascript - JS split text into sentences - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742375776a2463133.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论