Javascript RegExp for splitting text into sentences and keeping the delimiter

前端 未结 5 1993
青春惊慌失措
青春惊慌失措 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:23

    Try this instead:-

    sentences = text.split(/[\\.!\?]/);
    

    ? is a special char in regular expressions so need to be escaped.

    Sorry I miss read your question - if you want to keep delimiters then you need to use match not split see this question

提交回复
热议问题