Javascript 如何按出现的第一个数字将字符串一分为二?

Javascript 如何按出现的第一个数字将字符串一分为二?,javascript,regex,split,substring,Javascript,Regex,Split,Substring,有这样一根绳子 str = "word 12 otherword(s) 2000 19" str = "word word 12 otherword(s) 2000 19" 还是像这样 str = "word 12 otherword(s) 2000 19" str = "word word 12 otherword(s) 2000 19" 我需要将字符串一分为二,以获得如下数组: newstr[0]=字符串

有这样一根绳子

str = "word 12 otherword(s) 2000 19"
str = "word word 12 otherword(s) 2000 19"
还是像这样

str = "word 12 otherword(s) 2000 19"
str = "word word 12 otherword(s) 2000 19"
我需要将字符串一分为二,以获得如下数组:

newstr[0]=字符串的第一部分,即第一种情况下的单词,第二种情况下的单词

newstr[1]=字符串的其余部分,即12个其他单词2000 19

我曾尝试使用split和regex来完成此任务,但没有成功:

str.split/\d.*/返回数组[word]或数组[word]

而str.split/^\D*/gm返回数组[,12其他单词2000 19]


你能给我一个建议吗?即使不使用split和regex-如果有更好/更快的香草JavaScript解决方案。

您可以匹配以下部分:

const strs=[单词12其他单词2000 19,单词12其他单词2000 19]; strs的var s{ const[\ux,part1,part2]=s.match/^\D*\D+[\w\w]*/ console.log[第1部分,第2部分]
} 这里有三件事

String.split通常不在返回数组中包含匹配的分隔符。所以拆分abc.split'b'将返回['a','c']。使用匹配的正则表达式组可以更改此行为;i、 e.添加参数“abc”。拆分/b/将返回['a','b','c']

String.split将使delimter与其他元素分离。即“abc”。拆分/b/将返回3个元素['a'、'b'、'c']。将正则表达式后缀为。*以组合最后两个元素:“abc”。拆分/b.*/将返回['a','bc',]

最后,为了忽略最后一个空元素,我们发送第二个参数2

设str=单词12,其他单词2000 19; 设splitStr=str.split/\d.*/,2; console.logsplitStr;