如何将文本解析为特定的数组列表?

我有以下字符串:

const text = '01. is simply dummy text of the printing. 02. It is a long established fact that a reader will be. 03. and web page editors now use Lorem Ipsum.';

我需要将其解析为以下数组列表:

[
  '01. is simply dummy text of the printing.',
  '02. It is a long established fact that a reader will be.',
  '03. and web page editors now use Lorem Ipsum.'
]

我已经尝试过这种方法,但是没有得到想要的结果:

const list = text.match(/\d+.\s(.*)./g);

[
  '01. is simply dummy text of the printing. 02. It is a long established fact that a reader will be. 03. and web page editors now use Lorem Ipsum.'
]

请您帮我解决这个问题。

评论
  • 安蓝儿
    安蓝儿 回复

    如果我们可以假设句子中没有数字,那么您可以寻找不包含数字且以句号结尾的任何内容。

    Also a . in regex means "match anything", if you want a proper full stop, you need to escape it \.

    const text = '01. is simply dummy text of the printing. 02. It is a long established fact that a reader will be. 03. and web page editors now use Lorem Ipsum.';
    
    const list = text.match(/(\d+\.[^\d]+\.)/g);
    
    console.log(list)
  • w无意
    w无意 回复

    您可以匹配数字,后跟一个点,直到第一次出现一个点,然后是空格或字符串的末尾。

    \d+\.\s.*?\.(?= |$)
    

    Regex demo

  • 相思浓
    相思浓 回复

    您可以使用

    /\d+\..*?\.(?=\s*\d+\.|$)/g
    

    细节

    • \d+ - 1+ digits
    • \. - a dot (must be escaped)
    • .*? - any 0 or more chars other than line break chars as few as possible
    • \. - a dot...
    • (?=\s*\d+\.|$) - that is followed with 0+ whitespaces, 1+ digits and a dot or end of string.

    参见JS演示:

    const text = '01. is simply dummy text of the printing. 02. It is a long established fact that a reader will be. 03. and web page editors now use Lorem Ipsum.';
    console.log(text.match(/\d+\..*?\.(?=\s*\d+\.|$)/g));