本文共 7860 字,大约阅读时间需要 26 分钟。
正则表达式并不难,只是很多人用得少所以记不住,但并不能否认它的强大之处!
字符 | 描述 |
---|---|
. | 匹配除换行符外的任意字符,一个点只能匹配一个 |
\w | 匹配字母数字下划线或者汉字 |
\W | 匹配任意不是字母,数字,下划线,汉字的字符 |
\s | 匹配空白字符,它和[ \t\n\r]等价(分别是空格制表符换行符回车符) |
\S | 匹配任意不是空白符的字符 |
\d | 匹配数字(只能表示一位数字,可以替换为[0-9]) |
\D | 匹配任意非数字的字符 |
\b | 匹配单词的开始或结束 |
\B | 匹配不是单词开头或结束的位置 |
^ | 匹配字符串的开始,在列举的上面表示非,如[^\d] |
a-Z | 匹配a-Z之间的任意字符 |
$ | 匹配字符串的结束 |
* | 重复零次或多次 |
+ | 重复一次或更多次 |
? | 重复零次或一次 |
{n} | 重复n次 |
{n,} | 重复n次或更多次 |
{n,m} | 重复n到m次 |
() | 表示分组,就是括号里面的作为一个整体 |
[] | 表示字符串的列举 |
let str = 'javascript'let reg = /java/// 检测字符串是否包含'java'console.log(reg.test(str)); //true
var str="1 plus 2 equal 3"console.log(str.match('equal')); //[ 'equal', index: 9, input: '1 plus 2 equal 3', groups: undefined ]console.log(str.match(/\d+/)); //[ '1', index: 0, input: '1 plus 2 equal 3', groups: undefined ]console.log(str.match(/\d+/g)); //[ '1', '2', '3' ]
stringObject.replace(regexp/substr,replacement)
var str="Visit Microsoft!"console.log(str.replace('Microsoft', "W3School")); //Visit W3School!console.log(str.replace(/Microsoft/, "W3School")); //Visit W3School!
let str="1 plus 2 equal 3"let reg = /\d/gconsole.log(reg.exec(str)); //[ '1', index: 0, input: '1 plus 2 equal 3', groups: undefined ]console.log(reg.exec(str)); //[ '2', index: 7, input: '1 plus 2 equal 3', groups: undefined ]console.log(reg.exec(str)); //[ '3', index: 15, input: '1 plus 2 equal 3', groups: undefined ]console.log(reg.exec(str)); //null
11位数
第1位为1,第2位在(3,4,5,6,7,8,9)中选1位,3~11位为数字即可
function isPhoneNumber(tel) { var reg =/^1[3-9]\d{9}$/; return reg.test(tel);}isPhoneNumber('13997584510')
function isEmail(str){ var reg = /^[A-Za-z0-9]+[-\.]*@[A-Za-z0-9]+(-[A-Za-z0-9])*\.[A-Za-z0-9]+$/; return reg.test(str);}isEmail('2270877057@qq.com')
邮箱由于格式种类众多,因此想用一个正则能够检测出所有的邮箱使不现实的,这使得邮箱的检测必定不会完美
按照密码强度标准,根据打分机制评定密码强度
长度(25分)
字母组成(25分)
数字组成(20分)
其它特殊符号(@#$%^&*)(25分)
额外奖励(5分)
最后评分标准:
分数 | 等级 |
---|---|
>=90 | 非常安全 |
>=80 | 安全 |
>=70 | 非常强 |
>=60 | 强 |
>=50 | 一般 |
>=25 | 弱 |
>=0 | 非常弱 |
由于检测规则较为复杂,因此借用了方法判断
// 获取总分数function getTotalScore(password) { let totalScore = 0 const regLowLetter = /[a-z]+/g const regUpLetter = /[A-Z]+/g const regAllLetter = /([a-z]+[A-Z]+)|([A-Z]+[a-z]+)/g const regSpecialChar = /[@#$%^&*]+/g const specialCharNum = password.match(regSpecialChar) ? password.match(regSpecialChar)[0].length : 0 const regNumber = /\d*/g const numberLen = password.length - password.replace(regNumber, '').length const extral2 = /^(?=.*[a-zA-Z])(?=.*\d).*$/ const extral3 = /^(?=.*[a-zA-Z])(?=.*\d)(?=.*[@#$%^&*]).*$/ const extral5 = /^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&*]).*$/ // 检测字符长度 if (password.length >= 8) { totalScore += 25 } else if (5 <= password.length) { totalScore += 10 } else { totalScore += 5 } // 检测字母 if (regAllLetter.test(password)) { totalScore += 25 } else if (regLowLetter.test(password) || regUpLetter.test(password)) { totalScore += 10 } else { totalScore += 0 } // 检测数字 if (numberLen >= 3) { totalScore += 20 } else if (0 < numberLen) { totalScore += 10 } else { totalScore += 0 } // 检测其他字符 if (specialCharNum == 1) { totalScore += 10 } else if (specialCharNum > 1) { totalScore += 25 } else { totalScore += 0 } // 额外奖励 if (extral5.test(password)) { totalScore += 5 } else if (extral3.test(password)) { totalScore += 3 } else if (extral2.test(password)) { totalScore += 2 } else { totalScore += 0 } return totalScore}// 获取级别function getRank(password) { let totalScore = getTotalScore(password) switch (true) { case totalScore >= 90: console.log('密码非常安全'); break; case totalScore >= 80: console.log('密码安全'); break; case totalScore >= 70: console.log('密码非常强'); break; case totalScore >= 60: console.log('密码强'); break; case totalScore >= 50: console.log('密码一般'); break; case totalScore >= 25: console.log('密码弱'); break; case totalScore >= 0: console.log('密码非常弱'); break; }}getRank('ahhi1233217890#0')
trim()
方法function replaceText(text) { var reg = /^\s*|\s*$/; return text.replace(reg, '')}replaceText(' Jfsd4324--_ ');
let text = 'Windows 1.03 and Windows 2.0 fisrt Released in 1985 and 1987 respectively.Windows 95 and Windows 98 are the successor.Then Windows 2000 and Windows Xp appeared.Windows Vista is the Latest version of the family.'function extractTetx(text) { let reg = /Windows ([\d.]|[a-zA-Z])+\b/g return text.match(reg)}console.log(extractTetx(text));
身份证的规则:
ai = xxxxxx yymmdd aa g
ai = xxxxxx yyyymmdd aa g p
x
是6位是地区编码,地区编码首位不能为0
ymd
分别表示年月日,表示出生日期,aa
是随机码,g是性别,p是校验码
校验码计算规则:
前17位号码加权因子为 Wi = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ]
验证位 tt = [ 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2 ]
校验码 p = tt[i]
,其中 ,i是公式除11后取得的余数,如2.31则 i=3,2.37 则 i =4
由于检测规则较为复杂,因此借用了方法判断
// 判断是否是身份证的核心方法function isIDCard(str) { let getBirthday // 获取身份证最后一位数,用于核对校验码 let getLastNum = str.slice(-1, ) if (str.length == 15) { getBirthday = str.slice(6, 12) // 出生日期核对(15位身份证无校验码) if (!(birthRule(getBirthday))) { return false } } else { getBirthday = str.slice(6, 14) // 校验码与生出日期核对 if (!(birthRule(getBirthday) && countLastNum(str) == getLastNum)) { return false } } // 正则核对 let reg = /(^[1-9]\d{13}(\d{3}[Xx]|\d{4})$)|(^[1-9]\d{11}(\d{3}[Xx]|\d{4})$)/; return reg.test(str);}// 判断生日是否合法(string:birthday)function birthRule(birthday) { let year = birthday.slice(0, -4) let month = birthday.slice(-4, -2) let day = birthday.slice(-2, ) if (year.length === 2) { year = '19' + year } if (year > new Date().getFullYear() || month > 12) { return false } let days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] // 闰年 if ((year % 100 !== 0 && year % 4 === 0) || year % 400 === 0) { days[1] = 29 } if (day > days[month - 1]) { return false } return true}// 计算校验码(即使用上述公式)function countLastNum(idCard) { let tt = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2] let wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2] let arr1 = [] let totalNum = 0 let arr2 = idCard.slice(0, 17).split('') arr2.forEach((item, i) => { arr1.push(parseInt(item) * wi[i]) }); totalNum = arr1.reduce((total, value) => total + value) return tt[mod(totalNum, 11)] - 1}// 取余函数function mod(den, mol) { return parseInt((den / mol).toFixed(1).toString().split('.')[1])}console.log(isIDCard('422301196912307153'));
function isQQ(qq) { var reg = /^[1-9]\d{4,10}$/; return reg.test(qq);}console.log(isQQ('13997584510'));
function isWeiXin(weiXin) { var reg = /^[a-zA-Z][a-zA-Z0-9_-]{5,19}$/; return reg.test(weiXin);}console.log(isWeiXin('J13997584510'));
转载地址:http://ugxoz.baihongyu.com/