简体   繁体   English

如何使用 ( ) [ ] 大括号允许作为密码正则表达式中的特殊字符?

[英]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.

相关问题 正则表达式允许数字或特殊字符 - Regular expression to allow either number or special characters 如何在JavaScript中使用包含特殊字符的正则表达式 - How to use a Regular Expression in JavaScript which contains special characters javascript正则表达式,至少3个字并允许特殊字符 - javascript regular expression of minimum 3 words and allow special characters 使用xregexp的javascript正则表达式替换特殊字符,但允许白名单 - javascript regular expression to replace special characters, but allow a whitelist, using xregexp 正则表达式,允许字母数字与空格并禁止某些特殊字符(&lt;&gt;&;) - Regular expression to allow alphanumeric with space and disallow certain special characters(<>&;) Javascript 具有特殊字符的正则表达式密码验证 - Javascript regular expression password validation having special characters 如何使用与Java脚本特殊符号,字符和数字正则表达式来验证密码 - How to Validate the password using regular expression with Special Symbols, Characters and Numeric in Java Script 特殊字符密码的正则表达式 - regular expression for password for special character 如何使用jQuery正则表达式验证字符,数字和特殊字符? - How to validate characters, numbers and special characters using jQuery regular expression? 数字和特殊字符的正则表达式 - Regular Expression For Numbers and Special Characters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM