我有以下字符串:
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\.
您可以匹配数字,后跟一个点,直到第一次出现一个点,然后是空格或字符串的末尾。
Regex demo
您可以使用
细节
\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演示: