gpt4 book ai didi

javascript - 连接数组值

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

我有这个字符串:

Item 1Item 2Item 3Item 4

我想得到:

["Item 1", "Item 2", "Item 3", "Item 4"]

我试着这样做:

var string = 'Item 1Item 2Item 3Item 4'
var regex = string.split(/(\d)/)
regex.splice(-1, 1)
regex[regex.length - 2] += regex[regex.length - 1];
regex.splice(-1, 1)
console.log(regex);

但它不起作用,知道如何获得所需的结果吗?

编辑:

max 处的字符串可能如下所示:

Item 1Item 2Item 3Item 4Item NItem N-1

最佳答案

注意:答案处理原始案例 - 更新前

使用String.match()查找以数字序列 ( regex101 ) 结尾的非数字序列:

var string = 'Item 1Item 2Item 3Item 4'
var arr = string.match(/\D+\d+/g)
console.log(arr);

关于javascript - 连接数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54257418/

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