正则表达式总结(持续更新中...)


项目中常用的正则
更新于 2021年10月12日 17:37:07

邮箱

const reg1 = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/

const reg2 = [\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?

手机号码

const reg1 = /^1[34578]{1}\d{9}$/                  
const reg2 = /^1[34578]\d{9}$|^0\d{9,10}$/   
const reg3 = /^0?(13[0-9]|15[012356789]|18[012346789]|14[5678]|17[678]|170[059]|166|19[89])[0-9]{8}$/

固定电话

const reg = /^([0-9]{3,4}-{0,1})?[0-9]{7,8}$/

身份证号码

const reg = /^[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]$/

URL的校验

const reg = /^((https?|ftp|file):\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/

包含中文的字符串

const reg = ^[\\u4e00-\\u9fa5]{0,}$

正整数

const reg1 = /^[1-9][0-9]*$/
const reg2 = /^(0|\+?[1-9][0-9]*)$/

保留n位小数

function checkFloat(n) {
  return new RegExp(`^([1-9]+[\d]*(.[0-9]{1,${n}})?)$`)
}

// 保留2位小数
const floatReg = checkFloat(2)

const floatNum1 = 1234.5
console.log(floatReg.test(floatNum1)) // true

const floatNum2 = 1234.55
console.log(floatReg.test(floatNum2)) // true

const floatNum3 = 1234.555
console.log(floatReg.test(floatNum3)) // false

价格

const reg = /^(0|[1-9][0-9]*)($|\.[0-9]{1,2}$)/

10-99.99价格

const reg = /^(\d{1,2}(\.\d{1,2})?|99)$/

金额

精确到2位小数

const reg1 = /^[\u4E00-\u9FA5A-Za-z0-9]{1,5}$/ 

const reg2 = ^[0-9]+(.[0-9]{2})?$

0-100整数

const reg = /^(([0-9]\d?)|100)$/

0-10000整数

const reg = /^(0|[1-9][0-9]{0,3}|10000)$/

密码

否含有大小写字母、数字、特殊符号的两种及以上

const reg = /^(?![A-Z]+$)(?![a-z]+$)(?!\d+$)(?![\W_]+$)\S{8,16}$/

校验密码强度

密码的强度必须包含大小写字母和数字的组合,不能使用特殊字符,长度在8-10之间

const reg = ^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$

校验日期

// `yyyy-mm-dd` 格式的日期校验
const reg1 = /^\d{4}(\-)\d{1,2}\1\d{1,2}$/

// `YYYY-MM-DD hh:mm:ss` 格式的日期校验
const reg2 = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/

判断IE的版本

const reg = ^.*MSIE [5-8](?:\\.[0-9]+)?(?!.*Trident\\/[5-9]\\.0).*$

文件路径及扩展名校验

const reg = ^([a-zA-Z]\\:|\\\\)\\\\([^\\\\]+\\\\)*[^\\/:*?"<>|]+\\.txt(l)?$

IPv4的校验

const reg = /^(?:(?: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 reg= /^\d{6}$/

QQ号的校验

5-11位数字

const reg = /^[1-9][0-9]{4,10}$/

微信号的校验

6至20位,以字母开头,字母,数字,减号,下划线

const reg = /^[a-zA-Z]([-_a-zA-Z0-9]{5,19})+$/

车牌号的校验

const reg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/

只含字母的字符串

const reg = /^[a-zA-Z]+$/

包含中文的字符串

const reg = /[\u4E00-\u9FA5]/

字符串长度n的校验

function checkStrLength(n) {
  return new RegExp(`^.{${n}}$`)
}

// 校验长度为3的字符串
const lengthReg = checkStrLength(3)

const str1 = 'hhh'
console.log(lengthReg.test(str1)) // true

const str2 = 'hhhhh'
console.log(lengthReg.test(str2)) // false

文件拓展名的校验

function checkFileName (arr) {
  arr = arr.map(name => `.${name}`).join('|')
  return new RegExp(`(${arr})$`)
}

const filenameReg = checkFileName(['jpg', 'png', 'txt'])

const filename1 = 'test.jpg'
console.log(filenameReg.test(filename1)) // true
const filename2 = 'test.png'
console.log(filenameReg.test(filename2)) // true
const filename3 = 'test.txt'
console.log(filenameReg.test(filename3)) // true
const filename4 = 'test.md'
console.log(filenameReg.test(filename4)) // false
posted @ 2022-07-20 18:17  猫老板的豆  阅读(60)  评论(0编辑  收藏  举报