正则表达式在 Javascript 中的 url 上

Regex on url in Javascript

本文关键字:url 中的 Javascript 正则表达式      更新时间:2023-09-26

尽管我努力了,但我从来都不擅长正则表达式。有人可以帮我一下,我正在尝试匹配这种格式的网址:

"/year/monthnum/day/postname/"

所以:

"2012/05/03/whatever-the-text-here"

用:

if(window.location.pathname.match(...))
{
 //do something
}

我已经尝试了几件事,但我对正则表达式很糟糕......有人可以在这里帮助我吗?

http://regexr.com?30ram

if(window.location.pathname.match(/'/?[0-9]{4}'/[0-9]{1,2}'/[0-9]{1,2}'/.+$/))

这也可以增强以检查那一天不是 0... 或类似的事情......但我认为这就足够了。

试试这个:

if (/(?:(?:'d{4})['''/](?:'d{2})['''/](?:'d{2})['''/])([^'''/]+)/m.test(subject)) {
    // Successful match
} else {
    // Match attempt failed
}

愿这个网址在URL正则表达式中为您提供更多帮助

http://regexlib.com/Search.aspx?k=URL&AspxAutoDetectCookieSupport=1