简体   繁体   English

正则表达式捕获第一个零

[英]Catch first zero by regex

I have a string: 我有一个字符串:

0012309test07

and I want to find the first zeros and letters. 我想找到第一个零和字母。

00 12309 test 07 00 12309 测试 07

Sure I found letters with /[^\\d]/ , here's the sample . 当然,我发现带有/[^\\d]/字母, 这是示例

But how do you catch the first zeros ? 但是,如何捕获前零?

使用此正则表达式\\b0+|[a-zA-Z]+

使用这种模式

(^(0*))|([a-zA-Z]+)

"0012309test07".match(/^(0*)(\\d*)([^\\d]+)(\\d*)$/)

output: ["0012309test07", "00", "12309", "test", "07"] 输出: ["0012309test07", "00", "12309", "test", "07"]

"12309test07".match(/^(0*)(\\d*)([^\\d]+)(\\d*)$/)

output: ["12309test07", "", "12309", "test", "07"] 输出: ["12309test07", "", "12309", "test", "07"]

"test07".match(/^(0*)(\\d*)([^\\d]+)(\\d*)$/)

output: ["test07", "", "", "test", "07"] 输出: ["test07", "", "", "test", "07"]

"0test07".match(/^(0*)(\\d*)([^\\d]+)(\\d*)$/)

output: ["0test07", "0", "", "test", "07"] 输出: ["0test07", "0", "", "test", "07"]

"1test07".match(/^(0*)(\\d*)([^\\d]+)(\\d*)$/)

output: ["1test07", "", "1", "test", "07"] 输出: ["1test07", "", "1", "test", "07"]

"test".match(/^(0*)(\\d*)([^\\d]+)(\\d*)$/)

output: ["test", "", "", "test", ""] 输出: ["test", "", "", "test", ""]

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

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