0

次のような一般的な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 
  ...
  ...
4

2 に答える 2

3

先読みアサーションを使用する1つのソリューション:

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
 )
)
于 2012-06-26T10:46:58.187 に答える
1

文字をグループにプールする必要があります。その後、ORを使用して、文字列で発生するさまざまな順序で文字を受け入れることができます。どの注文でも機能する一般的な正規表現を作成することはできません。発生するすべての注文を明確に指定する必要があります。

于 2012-06-26T10:42:48.657 に答える