如何在JavaScript中创建正则表达式以删除字符串中的左零?
我有这个:
“ 2015年1月1日”
我需要获得这个:
“ 2015年1月1日”
如果这是约会,那么Zan的方法可能是更好的方法。但是,如果您确实想使用正则表达式来执行此操作,则可以采用以下方法:
要仅删除第一个前导零,请执行以下操作:
"01/01/2015".replace(/^0(.*)/,"$1")
更详细:
str = "01/01/2015"
pat = /^0(.*)/ // Match string beginning with ^, then a 0, then any characters.
str.replace(pat,"$1") // Replace with just the characters after the zero
要删除每个分组中的前导零,请执行以下操作:
str = "01/01/2015"
pat = /(^|[/])0(\d*)/g // Match string begin ^ or /, then a 0, then digits. 'g' means globally.
str.replace(pat,"$1$2") // Replace with the part before and after the 0.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句