正则表达式特定的重复间隔解析器,大多数工作

前端之家收集整理的这篇文章主要介绍了正则表达式特定的重复间隔解析器,大多数工作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下正则表达式:

once|every (?:(?:([1-9][0-9]?[0-9]? )?(hours?|days?))|(?:(?:(?:(1st|2nd|3rd|4th|5th))? ?(monday|tuesday|wednesday|thursday|friday|saturday|sunday)?)|((?:[1-9]|[1-2][0-9]|3[0-1])(?:st|nd|rd|th))) ?(?:(of the month)? ?(?:(?:repeated every )([2-9][0-9]?[0-9]?) (weeks|months))?)?)

这适用于我需要的大部分内容,但不适用于以下字符串…

Every 20th of the month

以下工作很棒……

Once
Every hour
Every day
Every 2 hours
Every 2 days
Every Monday
Every 1st of the month
Every 1st of the month repeated every 3 months
Every 2nd Monday of the month
Every 2nd Monday of the month repeated every 3 months

你能帮我吗?
谢谢

解决方法

问题是(?:(?:( 1st | 2nd | 3rd | 4th | 5th))?(星期一|星期二|星期三|星期四|星期五|星期六|星期日)?)部分:它可能匹配一个空字符串.因此,它总是“赢”.

您可以通过删除来修复它吗?星期日之后的量词,使星期几成为强制性模式.见this regex demo.

另外,如果你用正则表达式检查的字符串是独立的,你可以用^(?:和)$模式将整个模式括起来,使它们匹配整个模式.见this regex demo.

猜你在找的正则表达式相关文章