javascript - 连接数组值
问题描述
我有这个字符串:
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);
推荐阅读
- mesibo - 注册:使用 Mesibo 时,用户消息不会存储在本地
- typescript - Cloudformation 堆栈上的 AWS-CDK IAM 角色
- angular - 角度单元测试,超过代码行不计入结果
- maven - 在远程服务器上构建 maven 项目失败
- java - 有没有办法根据 SQL 查询锁定块执行?
- validation - 谷歌表格在下拉列表中出现无效错误,但项目在列表中
- gitea - Gitea - 不允许创建存储库 api 响应方法
- react-native - 反应本机导航抽屉:按下叠加层时抽屉未关闭
- r - 跨多列使用 IFELSE 函数
- javascript - 如果没有,我真的不明白这一点!操作员