gpt4 book ai didi

javascript - 字符串中的连续数字到数组

转载 作者:行者123 更新时间:2023-11-30 11:21:35 24 4
gpt4 key购买 nike

我有一个包含序列号的字符串,如下所示:

1. this is some text 2. this is more text 3. this is even more text

我想把它拆分成一个以数字开头的数组:

['1. this is some text', '2. this is more text', '3. this is even more text']

我尝试了以下方法:

string.split(/(?=\d[.][ ])/g);

这在数字为 1-9 时效果很好。但是,当数字 >=10 时,效果不佳。

关于如何让它发挥作用的任何想法?

最佳答案

是的,这就是您需要更改的全部内容(尝试 JSFiddle ):

string.split(/(?=\b\d+[.] )/g);

\d 之后的 + 告诉它匹配 1 个或多个。

编辑:\b 是一个单词边界,以确保您从头开始获取数字,而不是每个单独的数字。

关于javascript - 字符串中的连续数字到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49653482/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com