[英]regular expression alphanumeric characters - allowing hyphens and underscores
I need a Regular Expression for Javascript that checks if a password is correct only when 我需要一个Java正则表达式,仅在以下情况下才检查密码是否正确:
Examples: 例子:
I tried using 我尝试使用
var Reg = /^(?=.*\d)(?=.*[A-Za-z])[0-9A-Za-z-_]{4,12}$/;
It works in PHP, but not in Javascript, any suggestions? 它可以在PHP中运行,但不能在Javascript中运行,有什么建议吗?
Put the hyphen at the end of the character class: 将连字符放在字符类的末尾:
var Reg = /^(?=.*\d)(?=.*[A-Za-z])[0-9A-Za-z_-]{4,20}$/;
You can also shorten that character class: 您也可以缩短该字符类:
var Reg = /^(?=.*\d)(?=.*[A-Za-z])[\w-]{4,20}$/;
Also, you said 20 characters, not 12. Right? 另外,您说的是20个字符,而不是12个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.