Javascript 时间格式验证正则表达式,军用或AM/PM,但不能同时使用

Javascript 时间格式验证正则表达式,军用或AM/PM,但不能同时使用,javascript,regex,time-format,Javascript,Regex,Time Format,我这里有一个正则表达式: ^(\d{1,2}):(\d{2})(:00)?(\s)?(AM|PM|am|pm)?$ 它做得很好。允许23:00:00或12:00 AM或1:00 AM等内容 问题是它还允许像23:24am这样的东西 如果第一组数字大于12,修改或附加此项以禁用AM/PM的最佳方法是什么?尝试一下: ^(?:(?:(?:0?\d|1[012]):[0-5]\d(?::00)? ?[ap]m)|(?:[01]?\d|2[0-3]):[0-5]\d(?::00)?)$ 说明: ^

我这里有一个正则表达式:

^(\d{1,2}):(\d{2})(:00)?(\s)?(AM|PM|am|pm)?$
它做得很好。允许
23:00:00
12:00 AM
1:00 AM
等内容

问题是它还允许像
23:24am
这样的东西

如果第一组数字大于12,修改或附加此项以禁用AM/PM的最佳方法是什么?

尝试一下:

^(?:(?:(?:0?\d|1[012]):[0-5]\d(?::00)? ?[ap]m)|(?:[01]?\d|2[0-3]):[0-5]\d(?::00)?)$
说明:

^
(?:                 : start non capturing group
  (?:               : start non capturing group
    (?:             : start non capturing group
      0?\d          : matches optional 0 followed by 1 digit
      |             : OR
      1[012]        : 1 followed by digit between 0 and 2
    )               : end group
    :               : literally :
    [0-5]\d         : digit between 0 and 5 followed by any digit
    (?::00)?        : optional 00 seconds
     ?              : optional space
    [ap]m           : am or pm
  )                 : end group 
  |                 : OR
  (?:               : start non capturing group
   [01]?\d          : 0 or 1 (optional) followed by any digit
   |                : OR
   2[0-3]           : 20 to 23
  )                 : end group
  :                 : literally :
  [0-5]\d           : digit between 0 and 5 followed by any digit
  (?::00)?          : optional 00 seconds
)                   : end group
$                   
尝试一下:

^(?:(?:(?:0?\d|1[012]):[0-5]\d(?::00)? ?[ap]m)|(?:[01]?\d|2[0-3]):[0-5]\d(?::00)?)$
说明:

^
(?:                 : start non capturing group
  (?:               : start non capturing group
    (?:             : start non capturing group
      0?\d          : matches optional 0 followed by 1 digit
      |             : OR
      1[012]        : 1 followed by digit between 0 and 2
    )               : end group
    :               : literally :
    [0-5]\d         : digit between 0 and 5 followed by any digit
    (?::00)?        : optional 00 seconds
     ?              : optional space
    [ap]m           : am or pm
  )                 : end group 
  |                 : OR
  (?:               : start non capturing group
   [01]?\d          : 0 or 1 (optional) followed by any digit
   |                : OR
   2[0-3]           : 20 to 23
  )                 : end group
  :                 : literally :
  [0-5]\d           : digit between 0 and 5 followed by any digit
  (?::00)?          : optional 00 seconds
)                   : end group
$                   
尝试一下:

^(?:(?:(?:0?\d|1[012]):[0-5]\d(?::00)? ?[ap]m)|(?:[01]?\d|2[0-3]):[0-5]\d(?::00)?)$
说明:

^
(?:                 : start non capturing group
  (?:               : start non capturing group
    (?:             : start non capturing group
      0?\d          : matches optional 0 followed by 1 digit
      |             : OR
      1[012]        : 1 followed by digit between 0 and 2
    )               : end group
    :               : literally :
    [0-5]\d         : digit between 0 and 5 followed by any digit
    (?::00)?        : optional 00 seconds
     ?              : optional space
    [ap]m           : am or pm
  )                 : end group 
  |                 : OR
  (?:               : start non capturing group
   [01]?\d          : 0 or 1 (optional) followed by any digit
   |                : OR
   2[0-3]           : 20 to 23
  )                 : end group
  :                 : literally :
  [0-5]\d           : digit between 0 and 5 followed by any digit
  (?::00)?          : optional 00 seconds
)                   : end group
$                   
尝试一下:

^(?:(?:(?:0?\d|1[012]):[0-5]\d(?::00)? ?[ap]m)|(?:[01]?\d|2[0-3]):[0-5]\d(?::00)?)$
说明:

^
(?:                 : start non capturing group
  (?:               : start non capturing group
    (?:             : start non capturing group
      0?\d          : matches optional 0 followed by 1 digit
      |             : OR
      1[012]        : 1 followed by digit between 0 and 2
    )               : end group
    :               : literally :
    [0-5]\d         : digit between 0 and 5 followed by any digit
    (?::00)?        : optional 00 seconds
     ?              : optional space
    [ap]m           : am or pm
  )                 : end group 
  |                 : OR
  (?:               : start non capturing group
   [01]?\d          : 0 or 1 (optional) followed by any digit
   |                : OR
   2[0-3]           : 20 to 23
  )                 : end group
  :                 : literally :
  [0-5]\d           : digit between 0 and 5 followed by any digit
  (?::00)?          : optional 00 seconds
)                   : end group
$                   

以下是我解决问题的尝试:

^((0?[0-9]|1[0-2]):([0-5][0-9])(?::([0-5][0-9]))?)\s?([ap]m)?|([0-1][0-9]|[2][0-3]):([0-5][0-9])(?::([0-5][0-9]))?$
JS:

注意使用
i
选项(启用不区分大小写的匹配)和
m
匹配整行


此正则表达式允许分钟和秒(秒是可选的)。

以下是我解决此问题的尝试:

^((0?[0-9]|1[0-2]):([0-5][0-9])(?::([0-5][0-9]))?)\s?([ap]m)?|([0-1][0-9]|[2][0-3]):([0-5][0-9])(?::([0-5][0-9]))?$
JS:

注意使用
i
选项(启用不区分大小写的匹配)和
m
匹配整行


此正则表达式允许分钟和秒(秒是可选的)。

以下是我解决此问题的尝试:

^((0?[0-9]|1[0-2]):([0-5][0-9])(?::([0-5][0-9]))?)\s?([ap]m)?|([0-1][0-9]|[2][0-3]):([0-5][0-9])(?::([0-5][0-9]))?$
JS:

注意使用
i
选项(启用不区分大小写的匹配)和
m
匹配整行


此正则表达式允许分钟和秒(秒是可选的)。

以下是我解决此问题的尝试:

^((0?[0-9]|1[0-2]):([0-5][0-9])(?::([0-5][0-9]))?)\s?([ap]m)?|([0-1][0-9]|[2][0-3]):([0-5][0-9])(?::([0-5][0-9]))?$
JS:

注意使用
i
选项(启用不区分大小写的匹配)和
m
匹配整行




此正则表达式允许分钟和秒(秒是可选的)。

语言是什么?时间验证是一件复杂的事情。把它留给一个库。我在javascriptIt中使用它还允许
78:99:00am
看看,它是否符合您的要求?语言是什么?时间验证是一件复杂的事情。把它留给一个库。我在javascriptIt中使用它还允许
78:99:00am
看看,它是否符合您的要求?语言是什么?时间验证是一件复杂的事情。把它留给一个库。我在javascriptIt中使用它还允许
78:99:00am
看看,它是否符合您的要求?语言是什么?时间验证是一件复杂的事情。把它留给一个库。我在javascriptIt中使用它还允许
78:99:00 AM
看看,它符合你的要求吗?@Biffen:它起作用是因为OP希望秒数等于00。这绝对是一个改进。谢谢我也喜欢上面评论中的Stribizev链接。如果JS允许VERBOSE正则表达式,那将非常好,不是吗?:)我必须把它给你详细的分类。非常感谢。@Stribizev:你是说嵌入的评论吗?这在javascript中是允许的,请参阅@Biffen:OP希望秒数等于00,这绝对是一个改进。谢谢我也喜欢上面评论中的Stribizev链接。如果JS允许VERBOSE正则表达式,那将非常好,不是吗?:)我必须把它给你详细的分类。非常感谢。@Stribizev:你是说嵌入的评论吗?这在javascript中是允许的,请参阅@Biffen:OP希望秒数等于00,这绝对是一个改进。谢谢我也喜欢上面评论中的Stribizev链接。如果JS允许VERBOSE正则表达式,那将非常好,不是吗?:)我必须把它给你详细的分类。非常感谢。@Stribizev:你是说嵌入的评论吗?这在javascript中是允许的,请参阅@Biffen:OP希望秒数等于00,这绝对是一个改进。谢谢我也喜欢上面评论中的Stribizev链接。如果JS允许VERBOSE正则表达式,那将非常好,不是吗?:)我必须把它给你详细的分类。非常感谢。@Stribizev:你是说嵌入的评论吗?这在javascript中是允许的,事实上,你没有提到任何关于捕获组的事情,我不关心它们。。。但是,如果您需要捕获时间值的所有部分,我需要像您在注释中显示的那样扩展分钟和秒捕获组。我更新了以确保所有的部分现在都被捕获到组中。事实上,你没有提到任何关于捕获组的事情,我不关心他们。。。但是,如果您需要捕获时间值的所有部分,我需要像您在注释中显示的那样扩展分钟和秒捕获组。我更新了以确保所有的部分现在都被捕获到组中。事实上,你没有提到任何关于捕获组的事情,我不关心他们。。。但是,如果您需要捕获时间值的所有部分,我需要像您在注释中显示的那样扩展分钟和秒捕获组。我更新了以确保所有的部分现在都被捕获到组中。事实上,你没有提到任何关于捕获组的事情,我不关心他们。。。但是,如果您需要捕获时间值的所有部分,我需要像您在注释中显示的那样扩展分钟和秒捕获组。我进行了更新,以确保所有部件现在都被捕获到组中。