【发布时间】:2019-06-12 22:07:20
【问题描述】:
我有这个字符串:
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
【问题讨论】:
-
But I rather got :如But I'd rather get:? -
这是唯一需要处理的字符串还是有更多情况?
-
@treyBake 很抱歉造成混乱 - 将问题更新为更清晰。
-
使用
string.split(/(?!^)(?=Item)/) -
@JamesCoyle 更新了答案以添加另一个示例。
标签: javascript arrays regex