我有一个看起来像这样的数组:
var things = ["33bn", "2x", "Apple123"];
Run Code Online (Sandbox Code Playgroud)
如何将该数组转换为:
var things = ["33", "bn", "2", "x", "Apple", "123"];
Run Code Online (Sandbox Code Playgroud)
是否可以使用split和RegExp执行此操作?
我不确定我应该怎么做,也许我可以循环遍历数组并使用RegExp拆分每个项目然后将新数组的每个项目推入旧数组?
使用箭头功能的浏览器:
things.map(t => t.match(/\d+|[A-Za-z]+/g))
.reduce((x, y) => x.concat(y));
Run Code Online (Sandbox Code Playgroud)
var things = ["33bn", "2x", "Apple123"];
var result = things.map(t => t.match(/\d+|[A-Za-z]+/g))
.reduce((x, y) => x.concat(y));
console.log(result);Run Code Online (Sandbox Code Playgroud)