Javascript RegExp for splitting text into sentences and keeping the delimiter

核能气质少年 提交于 2019-12-17 06:38:30

问题


I am trying to use javascript's split to get the sentences out of a string but keep the delimiter eg !?.

So far I have

sentences = text.split(/[\\.!?]/);

which works but does not include the ending punctuation for each sentence (.!?).

Does anyone know of a way to do this?


回答1:


You need to use match not split.

Try this.

var str = "I like turtles. Do you? Awesome! hahaha. lol!!! What's going on????";
var result = str.match( /[^\.!\?]+[\.!\?]+/g );

var expect = ["I like turtles.", " Do you?", " Awesome!", " hahaha.", " lol!!!", " What's going on????"];
console.log( result.join(" ") === expect.join(" ") )
console.log( result.length === 6);



回答2:


The following is a small addition to Larry's answer which will match also paranthetical sentences:

text.match(/\(?[^\.\?\!]+[\.!\?]\)?/g);

applied on:

text = "If he's restin', I'll wake him up! (Shouts at the cage.) 
'Ello, Mister Polly Parrot! (Owner hits the cage.) There, he moved!!!"

giveth:

["If he's restin', I'll wake him up!", " (Shouts at the cage.)", 
" 'Ello, Mister Polly Parrot!", " (Owner hits the cage.)", " There, he moved!!!"]



回答3:


Try this instead:-

sentences = text.split(/[\\.!\?]/);

? is a special char in regular expressions so need to be escaped.

Sorry I miss read your question - if you want to keep delimiters then you need to use match not split see this question




回答4:


A slight improvement on mircealungu's answer:

string.match(/[^.?!]+[.!?]+[\])'"`’”]*/g);
  • There's no need for the opening parenthesis at the beginning.
  • Punctuation like '...', '!!!', '!?' etc. are included inside sentences.
  • Any number of square close brackets and close parentheses are included. [Edit: different closing quotation marks added]


来源:https://stackoverflow.com/questions/11761563/javascript-regexp-for-splitting-text-into-sentences-and-keeping-the-delimiter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!