Regular js for HH: MM time from 00:00 to 23:59

  • Like the simplest regular season, what's the problem? - Andrew NOP
  • No problem. Tightly did not work, read something a year ago. - Sergey
  • ^ ([01] \ d | 2 [0-3]):? ([0-5] \ d) $ Will this work? - Sergey
  • There are simpler /\d{2}:\d{2}/ - user210322
  • but it will not miss 25:99 I see 2 digits colon and 2 more digits? - Sergey

3 answers 3

 ^(?:0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$ 

Details:

  • ^ - beginning of line
    • (?: - the beginning of a non-seizing group
    • 0[0-9]|1[0-9]|2[0-3] - exciting trappings
    • ) - end of non-capturing group
    • : - colon character (separating hours and minutes)
    • [0-5] - an exciting submask of the number from 0 to 5
    • [0-9] - exciting submask of numbers from 0 to 9
  • $ - end of line

    If there are other data in addition to time in the line, then this regular schedule is more suitable.

     /(([2][0-3])|([0-1][0-9])):([0-5][0-9])/ 

    For example -

     console.log("bla-bla 23:17 bla-bla".replace(/(([2][0-3])|([0-1][0-9])):([0-5][0-9])/,'zzz')) 

       ^([01]\d|2[0-3]):([0-5]\d)$ 

      Format from 00:00 to 23:59

       /^([01]\d|2[0-3]):([0-5]\d)$/.test('22:10') // истина /^([01]\d|2[0-3]):([0-5]\d)$/.test('Бла 22:10 бла') // ложь