首页 > 解决方案 > Finding punctuation marks in text with string-methods

问题描述

how can I find out when a punctuation(?!;.) or "<" character comes in the string. I don’t want to use an array or compare any letter, but try to solve it with string methods. Something like that:

var text = corpus.substr(0, corpus.indexOf(".");

Ok, if I explicitly specify a character like a punct, it works fine. The problem with my parsing is that with a long text in a loop, I no longer know how a sentence ends, whether with question marks or exclamation points. I tried following, but it doesn’t work:

var text = corpus.substr(0, corpus.indexOf(corpus.search("."));

I want to loop through a long string and use every punctuation found to use it as the end-of-sentence character.

Do you know how can I solve my problem?

标签: javascriptregexstring

解决方案


Use a regular expression:

var text = corpus.split(/[(?!;.)<]/g);

推荐阅读