[英]How to use ( ) [ ] braces to allow as special characters in password regular Expression?
I am using the following regular expression for a password field in my project:我在我的项目中对密码字段使用以下正则表达式:
/^(?=.*\d)(?=.*[~!@#$%^&*)(_+-:[}="`])(?=.*[a-z])(?=.*[A-Z]).{8,}$/
My requirement is to allow at least我的要求是至少允许
1 Upper case 1 大写
1 Lower case 1 小写
1 Number 1 个
1 Special character 1 特殊字符
It should allow `~!@#$%^&*)(_+-:[}=" special characters. If I enter other characters it should not allow those.它应该允许`~!@#$%^&*)(_+-:[}=" 特殊字符。如果我输入其他字符,它不应该允许那些。
But it is allowing other characters like |\\
etc.但它允许其他字符,如
|\\
等。
I don't have much knowledge on regular expressions.我对正则表达式了解不多。 Where am I going wrong?
我哪里错了?
Problem is placement of an unescaped -
just after +
in your character class which is in the middle and acting like a range between +
ie 0x2b
and :
ie 0x3a
.问题是在你的字符类中放置一个未转义的
-
就在+
之后,它位于中间,就像+
即0x2b
和:
即0x3a
之间的范围。 All the characters in this range are allowed due to this.因此,允许此范围内的所有字符。
You can move -
to end of character class or at the start like this:您可以像这样移动
-
到字符类的末尾或开头:
/^(?=.*\d)(?=.*[~!@#$%^&*)(_+:[}="`-])(?=.*[a-z])(?=.*[A-Z])[~!@#$%^&*)(+:[}="`\w-]{8,}$/
Also important is to change DOT (any character) in the end to allowed set of characters as I've shown above.同样重要的是最后将 DOT(任何字符)更改为允许的字符集,如我上面所示。 RegEx Demo
正则表达式演示
Whatever you don't want to allow just put them in square brackets([]) with hat sign(^)无论您不想允许什么,只需将它们放在带有帽子符号(^)的方括号([])中
example :例子 :
[^|][^/]
This will not allow these characters.这将不允许这些字符。
Try this :尝试这个 :
Gt%:_=yh^|\5%3f
You need to escape the regex-specific characters ($,&,^,*,+,?,/,|,),(,],[,},{):您需要转义正则表达式特定的字符 ($,&,^,*,+,?,/,|,),(,],[,},{):
^(?=.*\d)(?=.*[\~\!\@\#\$\%\^\&\*\)\(\_\+\-\:\[\}\=\"\`])(?=.*[a-z])(?=.*[A-Z]).{8,}$
I usually just escape all punctuation to be safe, but there is no need to escape #, =, :,为了安全起见,我通常只是转义所有标点符号,但没有必要转义 #、=、:、
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.