[英]RegEx for Flexible Time Validation in JavaScript
I've begun with a regular expression for time validation, it should detect strings like: 我已经开始使用正则表达式进行时间验证,它应该检测如下字符串:
5s
3h5m
5h3m02s
05:03:02
05:03
4
My current regex is valid for all of these but one: 05:03 . 我目前的正则表达式对所有这些都有效但只有一个: 05 : 03 。 It detects 05 as hours and 03 as seconds , but it should be 05 as minutes... and I don't know how to edit my code to do this.
它将05 视为小时 , 03 视为秒 ,但它应该是05分钟......我不知道如何编辑我的代码来执行此操作。
var reTime =
/^(?:PT)?(?:(\d{1,2})[:.hH])?(?:(\d{1,4})[:.mM])?(?:(\d{1,6})[sS]?)?$/;
Test this one : 测试这一个:
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());
}
This works well (for example for "05:03:02,677") and if you wanna add something more, you can add easily. 这很好用(例如“05:03:02,677”),如果你想添加更多东西,你可以轻松添加。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.