JSFiddle - React, Tailwind, and code Playground

by kigorw

JavaScript

function split(text) {  
        var result=[], sentence="";
        for (var i=0; i < text.length; i++) {
            var s = text[i], s1 = text[i+1], s2 = text[i+2]; 
              sentence+=s;
            if(s+s1+s2=="...") {
                result.push(sentence);
                sentence = "";
                i+=2;
                continue;
            }
            if(".!?\n".indexOf(s)!=-1) {
                result.push(sentence);
                sentence = "";
            } 
          
            
        }
        result.push(sentence);
        return result;
        
}

function split2(text) {
        var re = /([.!?]\s+(?=[A-Z-—<])|\n+)/g, index,
            start = 0,
            result = []; 



        while ( (index = text.search(re)) !== -1 ) { 

            var match = text.match(re)[0];

            var end = index+match.length,
                s = text.substring(0, end);

            text = text.substring(end, text.length);

            var sentence = s;
            var se =    {
                value: s,
                s: start,//in case of updating text by substring
                e: start+end,
                id: 1
            }
            start+=end;
            result.push(sentence);
        }
        var sentence = text.substring(start, text.length);
        if(sentence.trim().length!=0) result.push(sentence);
        return result; 
        
    }                                                   

var r = split2("Some text. Some new tet. Some new tex? \n\ndsfserefasd \nYes. No.\nWhen\nAnd... Me.");

console.log(r);