只是稍微有点接触,有更好的解法欢迎指点
1.匹配密码,密码必须包含数字与字母
var pattern = /(?![^a-zA-Z]+$)(?!\D+$)/;
var str = '1a';
console.log(pattern.test(str));
2.匹配密码,密码强度:强,必须包含大写,小写和数字,和特殊字符(!,@,#,%,&),且大于6位
var pattern = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#%&])[A-Za-z0-9!@#%&]{6,}$/;
var str = '%111aaA!@';
console.log(pattern.test(str));
3.匹配email
var pattern = /^\w+@\w+(\.)\w+$/;
var str = '123@qq.com';
console.log(pattern.test(str));
4.匹配身份证
var pattern = /^[0-9]{17}([0-9]|x|X)$/;
var str = '12345678901234567x';
console.log(pattern.test(str));
5.匹配用户名,包含字母或者数字,且8位
var pattern = /^([A-Za-z0-9]|\w|([A-Za-z0-9]\W)){8}$/;
var str = '!!!!!!a_';
console.log(pattern.test(str));
6.匹配ip地址
var pattern = /^([0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$/;
var str = '168.0.255.57';
console.log(pattern.test(str));
7.计算一个字符串中所有的数字的和
例如 字符串是:‘hello90abc 78sjh12.5’ 结果是90 + 78 + 12.5 = 180.5
var pattern = /[^0-9.]/;
var str = 'h12llo90abc 78sjh12.5';
arr = str.split(pattern);
console.log(arr);
var result = 0;
for (var i = 0; i < arr.length; i++) {
arr[i] = parseFloat(arr[i]);
if (isNaN(arr[i])) {
arr[i] = 0;
}
result += arr[i];
}
console.log(arr);
console.log(result);