简体   繁体   English

从日期字符串中删除零

[英]Remove zeros from Date string

I have a string with the following format: '01/02/2016' and I am trying to get rid of the leading zeros so that at the end I get '1/2/2016' with regex. 我有一个字符串,格式如下:'01 / 02/2016',我试图摆脱前导零,以便最后我得到'1/2/2016'与正则表达式。

Tried '01/02/2016'.replace(/^0|[^\\/]0./, ''); 尝试'01/02/2016'.replace(/^0|[^\\/]0./, ''); so far, but it only gives me 1/02/2016 到目前为止,但它只给了我1/02/2016

Any help is appreciated. 任何帮助表示赞赏。

Replace \\b0 with empty string. 用空字符串替换\\b0 \\b represents the border between a word character and a non-word character. \\b表示单词字符和非单词字符之间的边界。 In your case, \\b0 will match a leading zero. 在您的情况下, \\b0将匹配前导零。

 var d = '01/02/2016'.replace(/\\b0/g, ''); console.log(d); 

You can use String.prototype.replace() and regular expression to replace the zero at the binning and the zero before / like this: 您可以使用String.prototype.replace()和正则表达式来替换binning中的零和之前/类的零:

 var d = '01/02/2016'.replace(/(^|\\/)0+/g, '$1'); console.log(d); 

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

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