- 手机号码的校验
复制 | const phoneReg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/ |
| |
| const phoneStr1 = '18886233487' |
| console.log(phoneReg.test(phoneStr1)) |
| |
| const phoneStr2 = '17283017203897' |
| console.log(phoneReg.test(phoneStr2)) |
- 身份证的校验
复制 | const sfzReg = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/ |
| |
| const sfzStr1 = '415106199801012130' |
| console.log(sfzReg.test(sfzStr1)) |
| |
| const sfzStr2 = '718381298381212183' |
| console.log(sfzReg.test(sfzStr2)) |
- 邮箱的校验
复制 | const emailReg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/ |
| |
| const emailStrWY = '956666@163.com' |
| const emailStrQQ = '956666@qq.com' |
| console.log(emailReg.test(emailStrWY)) |
| console.log(emailReg.test(emailStrQQ)) |
| |
| const noEmail = '72873213.com' |
| console.log(emailReg.test(noEmail)) |
- URL的校验
复制 | const urlReg = /^((https?|ftp|file):\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/ |
| |
| const urlStr1 = 'https://haha.sunshine.com/xxx/xxx' |
| console.log(urlReg.test(urlStr1)) |
| |
| const urlStr2 = 'sss://haha.sunshine.com/xxx/xxx' |
| console.log(urlReg.test(urlStr2)) |
- IPv4的校验
复制 | const ipv4Reg = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/ |
| |
| const ipv4Str1 = '122.12.56.65' |
| console.log(ipv4Reg.test(ipv4Str1)) |
| |
| const ipv4Str2 = '122.12.56.655' |
| console.log(ipv4Reg.test(ipv4Str2)) |
- 16进制颜色的校验
复制 | const color16Reg = /^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/ |
| |
| const color16Str1 = '#fff' |
| console.log(color16Reg.test(color16Str1)) |
| |
| const color16Str2 = '#1234567' |
| console.log(color16Reg.test(color16Str2)) |
- 日期 YYYY-MM-DD
复制 | const dateReg = /^\d{4}(\-)\d{1,2}\1\d{1,2}$/ |
| |
| const dateStr1 = '2021-10-10' |
| console.log(dateReg.test(dateStr1)) |
| |
| const dateStr2 = '2021-01-01 1' |
| console.log(dateReg.test(dateStr2)) |
- 日期 YYYY-MM-DD hh:mm:ss
复制 | const dateReg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/ |
| |
| const dateStr1 = '2021-10-10 16:16:16' |
| console.log(dateReg.test(dateStr1)) |
| |
| const dateStr2 = '2021-10-10 16:' |
| console.log(dateReg.test(dateStr2)) |
- 整数的校验
复制 | const intReg = /^[-+]?\d*$/ |
| |
| const intNum1 = 12345 |
| console.log(intReg.test(intNum1)) |
| |
| const intNum2 = 12345.1 |
| console.log(intReg.test(intNum2)) |
- 小数的校验
复制 | const floatReg = /^[-\+]?\d+(\.\d+)?$/ |
| |
| const floatNum = 1234.5 |
| console.log(floatReg.test(floatNum)) |
- 保留n位小数
复制 | function checkFloat(n) { |
| return new RegExp(`^([1-9]+[\d]*(.[0-9]{1,${n}})?)$`) |
| } |
| |
| const floatReg = checkFloat(2) |
| |
| const floatNum1 = 1234.5 |
| console.log(floatReg.test(floatNum1)) |
| |
| const floatNum2 = 1234.55 |
| console.log(floatReg.test(floatNum2)) |
| |
| const floatNum3 = 1234.555 |
| console.log(floatReg.test(floatNum3)) |
- 邮政编号的校验
复制 | const postalNoReg = /^\d{6}$/ |
| |
| const postalNoStr1 = '522000' |
| console.log(postalNoReg.test(postalNoStr1)) |
| |
| const postalNoStr2 = '5220000' |
| console.log(postalNoReg.test(postalNoStr2)) |
- QQ号的校验
说明:5-11位数字
复制 | const qqReg = /^[1-9][0-9]{4,10}$/ |
| |
| const qqStr1 = '1915801633' |
| console.log(qqReg.test(qqStr1)) |
| |
| const qqStr2 = '191580163333' |
| console.log(qqReg.test(qqStr2)) |
- 微信号的校验
说明:6至20位,以字母开头,字母,数字,减号,下划线
复制 | const wxReg = /^[a-zA-Z]([-_a-zA-Z0-9]{5,19})+$/ |
| |
| const wxStr1 = 'linsanxin885577' |
| console.log(wxReg.test(wxStr1)) |
| |
| const wxStr2 = '厉害了我的vx' |
| console.log(wxReg.test(wxStr2)) |
- 车牌号的校验
复制 | const carNoReg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/ |
| |
| const carNoStr1 = '粤A12345' |
| console.log(carNoReg.test(carNoStr1)) |
| |
| const carNoStr2 = '广东A12345' |
| console.log(carNoReg.test(carNoStr2)) |
- 只含字母的字符串
复制 | const letterReg = /^[a-zA-Z]+$/ |
| |
| const letterStr1 = 'sunshineLin' |
| console.log(letterReg.test(letterStr1)) |
| |
| const letterStr2 = 'sunshine_Lin' |
| console.log(letterReg.test(letterStr2)) |
- 包含中文的字符串
复制 | const cnReg = /[\u4E00-\u9FA5]/ |
| |
| const cnStr1 = '我是sunshine_Lin,林三心' |
| console.log(cnReg.test(cnStr1)) |
| |
| const cnStr2 = 'sunshine_Lin' |
| console.log(cnReg.test(cnStr2)) |
- 密码强度的校验
说明:密码中必须包含字母、数字、特称字符,至少8个字符,最多30个字符
复制 | const passwordReg = /(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{8,30}/ |
| |
| const password1 = 'sunshine_Lin12345..' |
| console.log(passwordReg.test(password1)) |
| |
| const password2 = 'sunshineLin12345' |
| console.log(passwordReg.test(password2)) |
- 字符串长度n的校验
复制 | function checkStrLength(n) { |
| return new RegExp(`^.{${n}}$`) |
| } |
| |
| |
| const lengthReg = checkStrLength(3) |
| |
| const str1 = 'hhh' |
| console.log(lengthReg.test(str1)) |
| |
| const str2 = 'hhhhh' |
| console.log(lengthReg.test(str2)) |
- 文件拓展名的校验
复制 | function checkFileName (arr) { |
| arr = arr.map(name => `.${name}`).join('|') |
| return new RegExp(`(${arr})$`) |
| } |
| |
| const filenameReg = checkFileName(['jpg', 'png', 'txt']) |
| |
| const filename1 = 'sunshine.jpg' |
| console.log(filenameReg.test(filename1)) |
| const filename2 = 'sunshine.png' |
| console.log(filenameReg.test(filename2)) |
| const filename3 = 'sunshine.txt' |
| console.log(filenameReg.test(filename3)) |
| const filename4 = 'sunshine.md' |
| console.log(filenameReg.test(filename4)) |
- 匹配img和src
复制 | const imgReg = /<img.*?src=[\"|\']?(.*?)[\"|\']?\s.*?>/ig |
| |
| const htmlStr = '<div></div><img src="sunshine.png" /><img src="sunshine111.png" />' |
| |
| console.log(imgReg.exec(htmlStr)) |
| |
| |
| |
| |
| |
| |
| |
| console.log(imgReg.exec(htmlStr)) |
| |
| |
| |
| |
| |
| |
| |
- 匹配html中的注释
复制 | const noteReg = /<!--(.*?)-->/g |
| |
| const htmlStr = '<!--一个div标签--> <div></div> <!--一个div标签--> <div></div>' |
| |
| console.log(noteReg.exec(htmlStr)) |
| |
| |
| |
| |
| |
| |
| |
| console.log(noteReg.exec(htmlStr)) |
| |
| |
| |
| |
| |
| |
| |
- 匹配html中的style
复制 | const styleReg = /([(\s+\w+=)|>])/g |
| |
| const htmlStr = '<div><span></span></div>' |
| |
| console.log(styleReg.exec(htmlStr)) |
| |
| |
| |
| |
| |
| |
| |
| console.log(styleReg.exec(htmlStr)) |
| |
| |
| |
| |
| |
| |
| |
- 匹配html中的颜色
复制 | const colorReg = /#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})/g |
| |
| const htmlStr = '<div><span></span></div>' |
| |
| console.log(colorReg.exec(htmlStr)) |
| |
| |
| |
| |
| |
| |
| |
| console.log(colorReg.exec(htmlStr)) |
| |
| |
| |
| |
| |
| |
| |
- 匹配htmlTag(html标签)
复制 | const endReg = /<("[^"]*"|'[^']*'|[^'">])*>/g |
| |
| const htmlStr = '<div><span></span></div><h1></h1>' |
| |
| console.log(endReg.exec(htmlStr)) |
| console.log(endReg.exec(htmlStr)) |
| console.log(endReg.exec(htmlStr)) |
| console.log(endReg.exec(htmlStr)) |
| console.log(endReg.exec(htmlStr)) |
| console.log(endReg.exec(htmlStr)) |
来源:https://www.fly63.com/article/detial/10745
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】博客园携手 AI 驱动开发工具商 Chat2DB 推出联合终身会员
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步