简体   繁体   English

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

[英]Regex splitting by first number

Note: This is NOT a duplicate of Regex to get first number in string: 100 2011-10-20 14:28:55 (only works when the string starts with a number). 注意:这不是Regex的重复项, 以获取字符串中的第一个数字:100 2011-10-20 14:28:55 (仅在字符串以数字开头时有效)。

Say I have the following input. 说我有以下输入。 The distribution of letters and numbers can be completely random. 字母和数字的分布可以完全随机。 Additionally, it may contain other characters such as - / ? 此外,它可能包含其他字符,例如- / ? etc.. 等等..

'ONEac123TWO45THREEabc67FOUR89bcFIVE'

What I need is an array in the form: 我需要的是以下形式的数组:

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

So for my example, that would be: 因此,以我的示例为例:

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

Thanks in advance :) 提前致谢 :)

You could use .match to get your result. 您可以使用.match获得结果。

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

How about: 怎么样:

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

http://rubular.com/r/kMhyKH3Sbd 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM