Javascript RegExp for splitting text into sentences and keeping the delimiter

前端 未结 5 1996
青春惊慌失措
青春惊慌失措 2020-11-28 08:31

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(/[\\\         


        
5条回答
  •  我在风中等你
    2020-11-28 09:32

    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);
    

提交回复
热议问题