且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在angular/javascript中使用格式(yyyy-dd-mm)在一天中修剪0,而不在月份中修剪0?

更新时间:2023-11-18 10:16:28

如果您只想删除当天的前导零,则:

If you just want to remove the leading zero from the day, then:

$scope.date.from.replace(/-0(\d)$/,'-$1')

可以.不需要前瞻或 g 标志(因为您只替换一个实例).

will do. There is no need for lookahead or the g flag (since you're only replacing one instance).

对不起,我应该解释正则表达式和替换的工作原理.

Sorry, I should have explained how the regular expression and replacement works.

在正则表达式中, -0(\ d)$ 匹配一个破折号,后跟一个零,然后是一个数字,然后是字符串的结尾.方括号()捕获保留空间中的匹配数字,该保留空间在替换字符串中由 $ 1 表示.

In the regular expression, -0(\d)$ matches a dash, followed by a zero, followed by a digit, followed by the end of the string. The brackets ( ) capture the matched digit in a hold space that is represented in the replacement string by $1.

然后将整个匹配项替换为替换字符串'-$ 1',它是破折号,后跟匹配的数字: $ 1 .

The whole match is then replaced by the replacement string '-$1' which is a dash, followed by the matched digit: $1.