for below code
var str = "I left the United States with my eyes full of tears! I knew I would miss my American friends very much.All the best to you";
var re = new RegExp("[^\.\?!]*(?:[\.\?!]+|\s$)", "g");
var myArray = str.match(re);
and This is what I am getting as a result
myArray[0] = "I left the United States with my eyes full of tears!"
myArray[1] = " I knew I would miss my American friends very much."
I want to add one more condition to regex such that the text will break only if there is a
space after the the punctuation mark (? or . or !)
I do it do that so the result for above case is
myArray[0] = "I left the United States with my eyes full of tears!"
myArray[1] = " I knew I would miss my American friends very much.All the best to you "
myArray[2] = ""
should work.
It will match any sequence of characters that are either
By using the reluctant quantifier
+?, it finds the shortest possible sequences (=single sentences).In JavaScript:
EDIT:
In order to avoid the regex splitting on “space/single letter or multidigit number/dot”, you can use:
This will split
into
What it does is instead of simply matching any character until it finds a dot is:
That way, the dot after a number/letter has already been matched and will not be matched as the delimiting punctuation character that follows next in the regex.