當前使用文本欄位,需要一些驗證才能捕獲一個月的日期。
^(?:[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]))?) )?$
我的正則運算式將允許像
1-30, 27, 3, 13.
但不是
10-27。
關于讓兩個示例正確評估的任何建議?
嘗試通過添加來搜索允許破折號的示例 - 但收到了相同的結果。
uj5u.com熱心網友回復:
您可以使用
^ # start of string
(?:[1-9]|[12][0-9]|3[01]) # a day pattern
(?: # start of an optional non-capturing group
\ *-\ * # a hyphen enclosed with zero or more spaces
(?:[1-9]|[12][0-9]|3[01]) # a day pattern
)? # end of the optional group
(?: # start of an optional non-capturing group
\ *,\ *(?:[1-9]|[12][0-9]|3[01]) # comma inside optional spaces day pattern
(?:\ *-\ *(?:[1-9]|[12][0-9]|3[01]))? # an optional sequence of hyphen inside optional spaces day pattern
)* # repeat zero or more times
$ # end of string
請參閱正則運算式演示。
一個班輪:
^(?:[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]))?)*$
請參閱此演示。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/519971.html
標籤:正则表达式
