gpt4 book ai didi

javascript - RegEx用于JavaScript中的灵活时间验证

转载 作者:行者123 更新时间:2023-12-03 13:07:48 25 4
gpt4 key购买 nike

我从用于时间验证的正则表达式开始,
它应该检测如下字符串:

5s
3h5m
5h3m02s
05:03:02
05:03
4

我当前的正则表达式对所有这些有效,但其中一种有效: 05:03 。它检测到 05 作为小时,检测到 03 作为秒,但是应该将05表示为分钟...,我不知道如何编辑代码来执行此操作。
 var reTime =
/^(?:PT)?(?:(\d{1,2})[:.hH])?(?:(\d{1,4})[:.mM])?(?:(\d{1,6})[sS]?)?$/;

最佳答案

测试一下:

var hasTimeFormat = function (input) {
var timeFormat = /([0-9]{2})\:([0-9]{2})\:([0-9]{2})\,([0-9]{3})$/;
if (typeof String.prototype.trim !== 'function') {
String.prototype.trim = function () {
return input.replace(/^\s+|\s+$/g, '');
}
}
return timeFormat.test(input.trim());
}

这样效果很好(例如,针对“05:03:02,677”),如果您想添加更多内容,则可以轻松添加。

关于javascript - RegEx用于JavaScript中的灵活时间验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27154328/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com