2016-04-09 10 views
0

正規表現の規則は、少なくとも8文字で、少なくとも次のいずれかを使用してください:(&%$ ^#@ =)最初または最後のNORにない番号は、パスワード内で隣り合っていてもよく、少なくとも2つの大文字と2つの小文字を持つことができます。これは私がこれまで持っていたものです。私はどのように文字の順序に注意しない正規表現を作る方法を把握することはできませんまた、私はすべてのものが8文字を必要とする方法を把握することはできません。正規表現を作成する必要があり、構文を理解できません。

/^[&%$^#@=]{1,}.[0-9(?!0-9)0-9(?!\s)]{2,}[A-Z]{2,}[a-z]{2,}$/; 
+1

[Lookaheads](http://www.regular-expressions.info/lookaround.html)は、問題を解決するのに役立ちます。私はあなたがGoogleによって数百万のパスワード検証正規表現を見つけ出し、あなたの要求にそれらを採用できるはずだと思います。 –

答えて

1

この正規表現は、これは、JavaScriptの関数はワットコメント正規表現が含まれてテストした

(?=^.{8,}$)(?!^\d)(?!.*\d$)(?!.+\d\d)(?=.*[&%$^#@=])(?=(.*[A-Z]){2})(?=(.*[a-z]){2})(?=(.*[0-9]){2}) 

Regex Demo

正規表現の内訳

(?=^.{8,}$) #Positive look ahead to check whether there are at least 8 characters 
(?!^\d) #Negative look ahead to check that string does not begin with a digit 
(?!.*\d$) #Negative look ahead to check that string does not end with a digit 
(?!.+\d\d) #Negative look ahead to check that string does not have two consecutive digits 
(?=.*[&%$^#@=]) #Positive look ahead to check that string have at least any of the characters present in character class 
(?=(.*[A-Z]){2}) #Positive look ahead to check that string contains at least two Upper Case characters 
(?=(.*[a-z]){2}) #Positive look ahead to check that string contains at least two Lower Case characters 
(?=(.*[0-9]){2}) #Positive look ahead to check that string contains at least two digits 
+0

ありがとうございます! –

+0

@GriffinObeid私は正規表現を更新しました。私はそれを誤読しました。それは私が正確に '8'と仮定していた少なくとも' 8'文字です..それを修正しました..また、少なくとも2桁の存在のチェックを加えました.. – rock321987

+0

'(?!^ \ d)(?!。* \ d $)(?!。+ \ d \ d)'と '(?* [0-9]){2})'パターンを '(?:\ D + \ d){2、} \ D + $'で終了することができます。 '(?=。{8、})'の代わりに '(?=。{8})'で十分です。一般に、キャプチャが不要な場合はキャプチャグループを作成せず、代わりに非キャプチャグループを使用してください。 –

3

を動作するようですこの正規表現は、文字列の先頭に位置する位置にも適用することができる方法を複数のAND論理条件を示していること

function valid_password(text) { 
    /*#!(?#!js re_password Rev:20160409_0700) 
     # Password validation with multiple requirements. 
     ^      # Anchor to start of string. 
     (?=[^&%$^#@=]*[&%$^#@=]) # Use at least one (&%$^#@=). 
     (?=(?:\D*\d){2})   # Have at least 2 numbers. 
     (?!\d)     # Numbers not at the beginning. 
     (?!.*\d$)     # Numbers not at the end. 
     (?=\D*(?:\d\D+)+$)  # Numbers not next to each other. 
     (?=(?:[^A-Z]*[A-Z]){2}) # At least 2 uppercase. 
     (?=(?:[^a-z]*[a-z]){2}) # At least 2 lowercase. 
     .{8,}      # Be at least 8 characters. 
     $       # Anchor to end of string. 
    !#*/ 
    var re_password = /^(?=[^&%$^#@=]*[&%$^#@=])(?=(?:\D*\d){2})(?!\d)(?!.*\d$)(?=\D*(?:\d\D+)+$)(?=(?:[^A-Z]*[A-Z]){2})(?=(?:[^a-z]*[a-z]){2}).{8,}$/; 
    if (text.match(re_password)) { return true; } 
    return false; 
} 

注:HICHは、あなたの検証要件を満たしています。上記の正規表現の数値要件の複数の条件は、単一のアサーションにまとめることができることにも注意してください。これらのアサーションは冗長(複数のアサーションスタイル)で示しています。

希望すると便利です。

+0

「OP」は「少なくとも2つの数字」を示しています。あなたの正規表現は1桁と2桁の文字列.https://regex101.com/r/xA1dM0/1 – rock321987

+0

@ rock321987 - いいえ、正規表現が正しく一致しています。あなたのregex101の例では、誤って "m"修飾子と複数行のテスト文字列を使用しています。 – ridgerunner

関連する問題