使用 JavaScript RegEx 使用 am 和 pm 验证时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18715392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Validate time with am and pm using JavaScript RegEx
提问by Karthiga
I am not very good with regex. I have the following time: 12:00am
.
我不太擅长正则表达式。我有以下时间:12:00am
.
I need a Javascript regex that respect this format: hh:mm[am/pm]
我需要一个尊重这种格式的 Javascript 正则表达式: hh:mm[am/pm]
var regex = /^(\d\d):(\d\d)\s?(?:AM|PM)?$/;
回答by leaf
You are almost done, the missing part is about the tens which never be greater than 1 for the hours and 5 for the minutes. I have also added the "ignore case" flag at the end which accepts "am", "AM", "Am", "aM":
你快完成了,缺少的部分是关于小时数永远不会大于 1 和分钟数永远不会大于 5 的十位。我还在末尾添加了“忽略大小写”标志,它接受“am”、“AM”、“Am”、“aM”:
var regex = /^([0-1]\d):([0-5]\d)\s?(?:AM|PM)?$/i;
A bit more restrictive (1 ≤ hour ≤ 12):
稍微严格一点(1 ≤ 小时 ≤ 12):
/^([1-9]|1[0-2]):([0-5]\d)\s?(AM|PM)?$/i
回答by Harry
The below RegEx should work. Demo Fiddle
下面的正则表达式应该可以工作。演示小提琴
var regex = /^([0]\d|[1][0-2]):([0-5]\d)\s?(?:AM|PM)$/i;
/* The below lines are just for demo testing purpose */
var timeStr = ["13:59am","11:59AM","09:69PM", "09:24pm", "09:99pm", "15:23bm", "09:23 AM", "14:74 PM"];
for (i=0; i<timeStr.length; i++)
console.log("Time String: "+timeStr[i]+ " Result: " +regex.test(timeStr[i]));
RegEx Explanation:
正则表达式说明:
([0]\d|[1][0-2])
- For Hours. Either 0
followed by any single digit (or) 1
followed by any single digit between 0-2
(that is, 0 or 1 or 2). This is to make sure that Hours value like 13 or 14 etc are treated as invalid (Since we are using time in AM/PM format, the Hours value should be a maximum of 12 only).
([0]\d|[1][0-2])
- 用了几个小时。任一0
随后的任何单个数字(或)1
随后之间的任何单个数字0-2
(即,0或1或2)。这是为了确保像 13 或 14 等这样的小时值被视为无效(因为我们使用 AM/PM 格式的时间,小时值应该最大为 12)。
([0-5]\d)
- For Minutes. Any single digit between 0-5
followed by another single digit between 0-9
. This is to make sure that minutes values like 64, 79 etc are treated as invalid.
([0-5]\d)
- 几分钟。之间的任何一位数字0-5
后跟之间的另一个一位数字0-9
。这是为了确保像 64、79 等分钟值被视为无效。
\s?
- 0 or 1 whitespace
\s?
- 0 或 1 个空格
(?:AM|PM)$/i
- Case insensitive AM or PM (that is am | pm | Am | Pm | aM | pM | AM | PM
)
(?:AM|PM)$/i
- 不区分大小写的 AM 或 PM(即am | pm | Am | Pm | aM | pM | AM | PM
)
Console Output:
控制台输出:
Time String: 13:59am Result: false Time String: 11:59AM Result: true Time String: 09:69PM Result: false Time String: 09:24pm Result: true Time String: 09:99pm Result: false Time String: 15:23bm Result: false Time String: 09:23 AM Result: true Time String: 14:74 PM Result: false
回答by Efrain Lugo
The below works I tested it using RegExr http://www.regexr.com/3btsp
下面的作品我使用 RegExr http://www.regexr.com/3btsp对其进行了测试
/^([0]?[1-9]|1[0-2]):([0-5]\d)\s?(AM|PM)$/i
/^([0]?[1-9]|1[0-2]):([0-5]\d)\s?(AM|PM)$/i
回答by Reza Nadimi
^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$