我要为严格的正则表达式0:59来99:59小时.它应该允许0到99小时.怎么可能呢.
/^([0-9]{1,2}(\:[0-5][0-9])?)$/
Run Code Online (Sandbox Code Playgroud)
工作正常
即使是简单的东西\d{1,2}(:[0-5]\d)?就足够了.
\d A digit
\d{1,2} One or two digits
\d{1,2}: One or two digits followed by :
\d{1,2}:[0-5] One or two digits followed by : followed by a digit 0 to 5
\d{1,2}:[0-5]\d ...followed by a digit (0 to 5) and another digit
\d{1,2}(:[0-5]\d)? ...making the :XX part optional due to the ?
Run Code Online (Sandbox Code Playgroud)
第二次更新:已修复以考虑可选:XX部分.