功能是校验密码必须包含 6-12 位字母和数字 确实能用
var rex = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$/;
后面的 [0-9A-Za-z]{6,12}$ 我能理解 但前面的两个分组 (?![0-9]+$) (?![a-zA-Z]+$) 是啥意思?
1
xujinkai 2017-01-12 15:01:52 +08:00 via Android
(?!)表示“后边的字符串不是” 所以既不是纯数字也不是纯字母 查一下正则规则就知道了
|
2
013231 2017-01-12 15:03:43 +08:00 1
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-negated-look-ahead
x(?!y) Matches 'x' only if 'x' is not followed by 'y'. This is called a negated lookahead. For example, /\d+(?!\.)/ matches a number only if it is not followed by a decimal point. The regular expression /\d+(?!\.)/.exec("3.141") matches '141' but not '3.141'. |
3
wlbcs OP |
4
Pastsong 2017-01-12 15:40:48 +08:00
|
5
lcorange 2017-01-12 15:46:20 +08:00
|
6
kevin1852 2017-01-12 15:57:41 +08:00
|
7
qyc666 2017-01-12 17:03:27 +08:00
负向前瞻
|
8
xujinkai 2017-01-12 17:21:48 +08:00 1
@wlbcs $是句尾,所以[0-9]+$表示到结尾都是数字,然后再用(?!)括起来,表示后边不能都是数字。我推荐 https://regex101.com/ 这个网站,多试试就知道了
|
9
h4x3rotab 2017-01-12 18:49:42 +08:00 via iPhone
写这种正则,还不如老老实实用 for 循环判断
|