【发布时间】:2021-06-08 13:14:52
【问题描述】:
有这样的字符串
str = "word 12 otherword(s) 2000 19"
或者像这样
str = "word word 12 otherword(s) 2000 19"
我需要将字符串一分为二,才能得到这样的数组:
newstr[0] = first part of the string(即第一种情况下的“word”,第二种情况下的“word word”);
newstr[1] = rest of the string(即“12 otherword(s) 2000 19”在这两种情况下)。
我尝试使用split 和regex 完成此操作,但没有成功:
str.split(/\d.*/) 返回Array [ "word ", "" ] 或Array [ "word word ", "" ]
而str.split(/^\D*/gm) 返回Array [ "", "12 otherword(s) 2000 19" ]
你能给我一个建议吗?即使不使用 split 和 regex - 如果有更好/更快(Vanilla JavaScript)的解决方案。
【问题讨论】:
标签: javascript regex split substring