如何在 JavaScript 中将数字列表拆分为数组列表

问题描述 投票:0回答:1

我有一个这样的字符串...

"1.first. 2.second. 3.third"

我希望使用 JavaScript split 让它像这样

["1.first.", "2.second", "3.third"]

我看到

C#
是这样的
Regex.Split(txt, "[0-9]+\\.");

但我需要 JavaScript 版本,请帮忙

我尝试使用这个... 描述.split(/([0-9]+)/)

但结果是这样的

[
"1", 
". first",
"2",
". second ",
"3",
".third",
"23",
"two three",
"4",
".four"
]
javascript split numbers
1个回答
0
投票

只需使用

Array::flatMap()
并使用带有后向断言的正则表达式分割单词:

console.log(["1.first.", "2.second", "3.third"].flatMap(item => item.split(/(?<=\.)/)));

© www.soinside.com 2019 - 2024. All rights reserved.