Split string into sentences in javascript

后端 未结 8 1077
悲哀的现实
悲哀的现实 2020-11-29 06:08

Currently i am working on an application that splits a long column into short ones. For that i split the entire text into words, but at the moment my regex splits numbers to

相关标签:
8条回答
  • 2020-11-29 06:47

    You could exploit that the next sentence begins with an uppercase letter or a number.

    .*?(?:\.|!|\?)(?:(?= [A-Z0-9])|$)
    

    Regular expression visualization

    Debuggex Demo

    It splits this text

    This is a long string with some numbers [125.000,55 and 140.000] and an end. This is another sentence. Sencenes beginning with numbers work. 10 people like that.
    

    into the sentences:

    This is a long string with some numbers [125.000,55 and 140.000] and an end.
    This is another sentence.
    Sencenes beginning with numbers work.
    10 people like that.
    

    jsfiddle

    0 讨论(0)
  • 2020-11-29 06:48

    I would just change the strings and put something between each sentence. You told me you have the right to change them so it will be easier to do it this way.

    \r\n
    

    By doing this you have a string to search for and you won't need to use these complex regex.

    If you want to do it the harder way I would use a regex to look for "." "?" "!" folowed by a capital letter. Like Tessi showed you.

    0 讨论(0)
提交回复
热议问题