Trying to build a regular expression to check pattern

后端 未结 7 745
我在风中等你
我在风中等你 2021-01-20 17:32

a) Start and end with a number
b) Hyphen should start and end with a number
c) Comma should start and end with a number
d) Range of number should be from 1-31

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-20 18:22

    How about this? This will check rules a, b and c, at least, but does not check rule d.

    /^[0-9]+(-[0-9]+)?(,[0-9]+(-[0-9]+)?)*$/

    If you need to ensure that all the numbers are in the range 1-31, then the expression will get a whole lot uglier:

    /^([1-9]|[12][0-9]|3[01])(-([1-9]|[12][0-9]|3[01]))?(,([1-9]|[12][0-9]|3[01])(-([1-9]|[12][0-9]|3[01]))?)*$/

    Note that your example c contains a number, 56, that does not fall within the range 1-31, so it will not pass the second expression.

提交回复
热议问题