我有一个字符串:
0012309test07
我想找到第一个零和字母。
00 12309测试07
当然,我找到了带有 的字母/[^\d]/
,这是样本。
但是你如何捕捉第一个零?
使用这个正则表达式\b0+|[a-zA-Z]+
使用这种模式
(^(0*))|([a-zA-Z]+)
"0012309test07".match(/^(0*)(\d*)([^\d]+)(\d*)$/)
输出:["0012309test07", "00", "12309", "test", "07"]
"12309test07".match(/^(0*)(\d*)([^\d]+)(\d*)$/)
输出:["12309test07", "", "12309", "test", "07"]
"test07".match(/^(0*)(\d*)([^\d]+)(\d*)$/)
输出:["test07", "", "", "test", "07"]
"0test07".match(/^(0*)(\d*)([^\d]+)(\d*)$/)
输出:["0test07", "0", "", "test", "07"]
"1test07".match(/^(0*)(\d*)([^\d]+)(\d*)$/)
输出:["1test07", "", "1", "test", "07"]
"test".match(/^(0*)(\d*)([^\d]+)(\d*)$/)
输出:["test", "", "", "test", ""]