这个正则花费了我四五个小时研究:
初始思路:String regex = "(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])[a-zA-Z0-9]{8,16}";
/** (?=.*[A-Z]) 必须有大写英文*/
/** (?=.*[a-z]) 必须有小写英文*/
/** (?=.*[0-9]) 必须有数字*/
/** [a-zA-Z0-9] 有效字符范围*/
/** {8,16} 最少8个字符,最多16个字符*/[/color]
第一个成功版本
var tx3=/^(?=.{1}.{7})(?!.{1}.{16,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[A-Za-z0-9]*[~@#\$%\*_\-\+\=\:,\.\?\[\]\{\}]{1}[A-Za-z0-9]*[~@#\$%\*_\-\+\=\:,\.\?\[\]\{\}]?[A-Za-z0-9]*[~@#\$%\*_\-\+\=\:,\.\?\[\]\{\}]?[A-Za-z0-9]*$/;
第二优化版本:
var tx3=/^(?=.{1}.{7})(?!.{1}.{16,})(?=.*[~@#\$%\*_\-\+\=\:,\.\?\[\]\{\}])(?!.*[~@#\$%\*_\-\+\=\:,\.\?\[\]\{\}]{3,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9~@#\$%\*_\-\+\=\:,\.\?\[\]\{\}]{8,16}$/;
优化最终版本:
var tx3=/^(?=.*[~@#\$%\*_\-\+\=\:,\.\?\[\]\{\}])(?!.*[~@#\$%\*_\-\+\=\:,\.\?\[\]\{\}]{3,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9~@#\$%\*_\-\+\=\:,\.\?\[\]\{\}]{8,16}$/;