首页 > 解决方案 > 连接数组值

问题描述

我有这个字符串:

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

标签: javascriptarraysregex

解决方案


注意:答案处理原始情况 - 更新之前

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

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


推荐阅读