简体   繁体   English

正则表达式 - 我做错了什么吗?

[英]Regex - is there something I've done wrong?

This is javascript, but a virtually identical regex is failing in PHP too, so I don't think it's language specific这是 javascript,但几乎相同的正则表达式在 PHP 中也失败了,所以我认为它不是特定于语言的

var r = new RegExp(
    "^(:19|20)?[0-9][0-9]"            // optional 19/20 start followed by 2 numbers
    + "-"                             // a hyphen
    + "(:0?[1-9]|1[0-2])"             // optional 0 followed by 1-9, or 10, 11, 12
    + "-"                             // a hyphen
    + "(:3[01]|[12][0-9]|0?[1-9])$"   // you get the idea.
);
r.test("2008-07-01");                // == false

What on earth am I missing?我到底错过了什么?

我认为你的非捕获块应该是例如(?:19|20)而不是(:19|20)

Correct;正确的; your regular expression would actually work with ?:您的正则表达式实际上适用于?:

? when used a prefix indicates that you're going to do something about capturing.使用前缀时表示您将要对捕获做一些事情。 Either not capture the block (:) , capture ahead (=) , behind (<=) , etc.要么不捕获块(:) ,捕获前面(=) ,后面(<=)等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM