gpt4 book ai didi

javascript - 正则表达式 - 不介意顺序吗?

转载 作者:行者123 更新时间:2023-11-28 16:17:00 25 4
gpt4 key购买 nike

我正在尝试创建一个可以接受一般 SQL/Js 日期的正则表达式,例如:

2012 年 2 月 31 日

我已经制作了一个正则表达式:

(0[1-9]|[12]\d|3[01])[ ]+(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)[ ]+[12]\d{3}$

但是我如何告诉正则表达式忽略项目的出现顺序:

所以它可以接受:

  Apr 31 2010
2010 Apr 31
...
...

最佳答案

使用先行断言的一种解决方案:

var myregexp = /^(?=.*(\b[A-Za-z]{3}\b))(?=.*(\b\d{1,2}\b))(?=.*(\b\d{4}\b))/;
var match = myregexp.exec(subject);
if (match != null) {
month = match[1];
days = match[2];
year = match[3];
}

说明:

^             # Start of string
(?= # Look ahead to see if the following can be matched:
.* # Any number of characters
( # followed by (capturing this in group no. 1)
\b # Start of word
[A-Za-z]{3} # Three ASCII letters
\b # End of word
) # End of capturing group no. 1
) # End of lookahead assertion.
(?= # Look ahead to see if the following can be matched:
.* # Any number of characters
( # followed by (capturing this in group no. 1)
\b\d{1,2}\b # a one- or two-digit number
) # etc.
)
(?= # Lookahead no. 3
.*
(
\b\d{4}\b # a four-digit number
)
)

关于javascript - 正则表达式 - 不介意顺序吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11205549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com