我想要一个正则表达式,检查“最少8个字符至少一个数字和一个特殊字符,最多32个字符.”,我的正则表达式是:
^(?=.*\d)(?=.*[a-zA-Z])[0-9a-zA-Z]{8,16}$
^(?=.*\d)(?=.*[a-zA-Z])[0-9a-zA-Z]{8,16}$
/(?=^.{8,32}$)(?=(?:.*?\d){1})(?=.*[a-z])(?=(?:.*?[!@#$%*()_+^&}{:;?.]){1})(?!.*\s)[0-9a-zA-Z!@#$%*()_+^&]*$/
你可以观看现场演示:http://jsfiddle.net/tuxrM/
var re = new RegExp(/(?=^.{8,32}$)(?=(?:.*?\d){1})(?=.*[a-z])(?=(?:.*?[!@#$%*()_+^&}{:;?.]){1})(?!.*\s)[0-9a-zA-Z!@#$%*()_+^&]*$/); var test1 = '1234567',// less than 8 characters test2 = 'aaaaaaaa',// no number & no special character test3 = 'aaaaaaa1',// no special character test4 = 'aaaAaa#1',// satisfied test5 = 'abcdefghigklmnopqrstuvwxyz1234567890332123'; // more than 32 characters re.test(test1); // FALSE re.test(test2); // FALSE re.test(test3); // FALSE re.test(test4); // TRUE re.test(test5); // FALSE