本文介绍了从时间格式中删除前导零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我收到一个这样格式的字符串'HH:mm:ss'
.我想删除前导零但始终保留最后四个字符,例如 m:ss
即使 m
将是零.我正在格式化音频时长.
I am receiving a string in this format 'HH:mm:ss'
. I would like to remove the leading zeros but always keeping the the last four character eg m:ss
even if m
would be a zero. I am formatting audio duration.
示例:
00:03:15
=> 3:15
10:10:10
=> 10:10:10
00:00:00
=> 0:00
04:00:00
=> 4:00:00
00:42:32
=> 42:32
00:00:18
=> 0:18
00:00:08
=> 0:08
00:03:15
=> 3:15
10:10:10
=> 10:10:10
00:00:00
=> 0:00
04:00:00
=> 4:00:00
00:42:32
=> 42:32
00:00:18
=> 0:18
00:00:08
=> 0:08
推荐答案
您可以使用此替换:
var result = yourstr.replace(/^(?:00:)?0?/, '');
或更好:
var result = yourstr.replace(/^0(?:0:0?)?/, '');
要处理 Matt 示例(见评论),您可以将模式更改为:
To deal with Matt example (see comments), you can change the pattern to:
^[0:]+(?=\d[\d:]{3})
这篇关于从时间格式中删除前导零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!