正则表达式按第一个数字分割

时间:2014-04-04 15:27:03

标签: javascript regex

注意:这不是Regex to get first number in string: 100 2011-10-20 14:28:55的副本(仅当字符串以数字开头时才有效)。

说我有以下输入。字母和数字的分布可以是完全随机的。此外,它可能包含其他字符,例如- / ?等。

'ONEac123TWO45THREEabc67FOUR89bcFIVE'

我需要的是一个表格中的数组:

[everything before the first number, the first number, everything else]

所以我的例子就是:

['ONEac','123','TWO45THREEabc67FOUR89bcFIVE']

提前致谢:)

2 个答案:

答案 0 :(得分:3)

您可以使用.match来获得结果。

> 'ONEac123TWO45THREEabc67FOUR89bcFIVE'.match(/^(\D*)(\d+)(.*)$/).slice(1)
  ["ONEac", "123", "TWO45THREEabc67FOUR89bcFIVE"]

答案 1 :(得分:2)

怎么样:

^(\D+)(\d+)(.+)$

http://rubular.com/r/kMhyKH3Sbd

^            start of string
(\D+)        match anything that is not a digit into one group (1 or more, use * for 0 or more)
(\d+)        match 1 or more digits into another group (I am assuming you want ints)
(.+)         match whatever is left into another group
$            end of string