admin管理员组文章数量:1334121
The current REGEX I'm using is the following one:
var sentences = fulltext.match(/[^\.!\?]+[\.!\?]+/g);
That returns an array with the sentences split INCLUDING the spaces (I need all the characters). Problem is, it does not work with ellipsis "..." and I guess neither it does with other unconventional forms of punctuation.
How can I fix my REGEX to match this and other forms of punctuation?
Is there any noob friendly example driven guide to REGEX out there?
The current REGEX I'm using is the following one:
var sentences = fulltext.match(/[^\.!\?]+[\.!\?]+/g);
That returns an array with the sentences split INCLUDING the spaces (I need all the characters). Problem is, it does not work with ellipsis "..." and I guess neither it does with other unconventional forms of punctuation.
How can I fix my REGEX to match this and other forms of punctuation?
Is there any noob friendly example driven guide to REGEX out there?
Share Improve this question edited Jan 26, 2014 at 1:53 BenMorel 36.7k51 gold badges205 silver badges336 bronze badges asked Jan 25, 2014 at 22:54 BelohlavekBelohlavek 1673 silver badges13 bronze badges 2-
2
Ellipsis also have their own character / code point -- U+2026 or
\u2026
-- that are distinct from 3 consecutive.
s (U+002E). – Jonathan Lonowski Commented Jan 25, 2014 at 22:58 - possible duplicate of Javascript regular expression for punctuation (international)? – Jonathan Lonowski Commented Jan 25, 2014 at 23:06
2 Answers
Reset to default 5Unicode of ellipsis is \u2026
.
So you can use \u2026
to match an ellipsis .
Code :
var fulltext= "First sentence… Second sentence. ";
fulltext.match(/([^.?!;\u2026]+[.?!;\u2026]+)/g);
OUTPUT
["First sentence…", " Second sentence."]
DEMO and Explanation
You can just add the ellipsis (and any other punctuation characters) to your character sets.
var input = "First sentence… Second sentence. ";
input.match(/[^\.\?!;…]+[\.\?!;…]+/g);
Result:
["First sentence…", " Second sentence."]
本文标签: Javascript Regex can39t match ellipsisStack Overflow
版权声明:本文标题:Javascript Regex can't match ellipsis - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742358851a2459957.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论