Angularjs MMMM dd,yyyy ng-pattern返回错误

时间:2016-07-06 17:53:21

标签: angularjs regex

我有一个字段设置为日期输入为值,但只要我输入日期,AngularJs就会在字段上返回错误。

这是模式的外观

ng-pattern='/^(?:(((Jan(uary)?|Ma(r(ch)?|y)|Jul(y)?|Aug(ust)?|Oct(ober)?|Dec(ember)?)\ 31)|((Jan(uary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|Aug(ust)?|Oct(ober)?|(Sept|Nov|Dec)(ember)?)\ (0?[1-9]|([12]\d)|30))|(Feb(ruary)?\ (0?[1-9]|1\d|2[0-8]|(29(?=,\ ((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))))\,\ ((1[6-9]|[2-9]\d)\d{2}))/'

我已经在 regexr 上测试了模式并且它正在运行。我正在测试的日期 2016年6月30日

1 个答案:

答案 0 :(得分:0)

我查看了你的正则表达式(经过深思熟虑),最后根据你的概念重新编写它。

这是我最终的结果:

((Jan(uary)?|Mar(ch)?|July?|Aug(ust)?|Oct(ober)?|Dec(ember)?) 31|(Jan(uary)?|Mar(ch)?|Apr(il)?|July?|June?|Aug(ust)?|Oct(ober)?|Sep|(Sept|Nov|Dec)(ember)?) (0?[1-9]|([12]\d)|30)|Feb(ruary)? (0?[1-9]|1\d|2[0-8]|29(?=, ((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(16|[2468][048]|[3579][26])00)))), (1[6-9]|[2-9]\d)\d{2}

以下是一个示例:https://regex101.com/r/gS0xB4/1

在这里它被打破了一点:

( // Begin get month and a day
    (Jan(uary)?|Mar(ch)?|July?|Aug(ust)?|Oct(ober)?|Dec(ember)?) 31 // Months with 31 days, and a 31
    | // OR
    (Jan(uary)?|Mar(ch)?|Apr(il)?|July?|June?|Aug(ust)?|Oct(ober)?|Sep|(Sept|Nov|Dec)(ember)?) // Every month but Feb, and...
    (0?[1-9]|([12]\d)|30) // A number from 1-30, optionally padded to 2 digits
    | // OR
    Feb(ruary)? // Feburary, and...
        (0?[1-9]|1\d|2[0-8] // A number from 1-28, optionally padded to 2 digits
        | // OR
        29(?=, ( // A 29, IF it is followed by a comma and...
            (1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26]) // A year which is a multiple of 4, but not a multiple of 100
            | // OR
            (16|[2468][048]|[3579][26])00) // A year which is a multiple of 400
        ) // End 29 conditional
    ) // End Feb
) // End month and day
, (1[6-9]|[2-9]\d)\d{2} // A comma, and a year between 1600 and 9999

我没有任何捕获组来分解它,因为我只是想让它匹配 - 但它们应该相当容易添加。我也删除了所有的转义,所以你可能需要添加如果引擎需要它们,它们会重新进入。