正则表达式集合,你值得拥有!

1,283 阅读1分钟

小知识,大挑战!本文正在参与“程序员必备小知识”创作活动。

大家好,我是孤独,今天整理一些项目中比较常用的正则表达式~~

这个正则表达式生成网站,我觉得还行,有兴趣的可以试试~

手机号码的校验

const phoneReg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;

const phone1 = '18565796021';
console.log(phoneReg.test(phoneStr1)); // true

const phone2 = '17283017203897';
console.log(phoneReg.test(phoneStr2)); // false

身份证的校验

const codecardReg = /^[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 code1 = '411222199710214566';
console.log(codecardReg.test(code1)); // true

const code2 = '41122219971021456624343';
console.log(codecardReg.test(code2)); // true

邮箱的校验

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

const email1 = '15037268535@163.com'; // 163邮箱
const email2 = '15037268535@qq.com'; // qq邮箱
console.log(emailReg.test(email1)); // true
console.log(emailReg.test(email2)); // true

const email3 = '32434545.com';
console.log(emailReg.test(email3)); // false

16进制颜色的校验

const colorReg = /^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/;

const color1 = '#fff';
console.log(colorReg.test(color1)) // true
const color2 = '#f1234567';
console.log(colorReg.test(color2)) // false

URL的校验

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

const urlStr1 = 'https://juejin.cn/user/1679709498507783/posts';
console.log(urlReg.test(urlStr1)) // true
const urlStr2 = 'uejin.cn/user/1679709498507783/postsx';
console.log(urlReg.test(urlStr2)) // false

日期 YYYY-MM-DD的校验

const dateReg = /^\d{4}(-)\d{1,2}\1\d{1,2}$/;

const dateStr1 = '2021-10-22';
console.log(dateReg.test(dateStr1)) // true
const dateStr2 = '2021-10-22 3';
console.log(dateReg.test(dateStr2)) // false

整数的校验

const intReg = /^[-+]?\d*$/;

const intNum1 = 11111;
console.log(intReg.test(intNum1)) // true
const intNum2 = 154.1;
console.log(intReg.test(intNum2)) // false

小数的校验

const floatReg = /^[-+]?\d+(.\d+)?$/;

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

保留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 postalNoReg = /^\d{6}$/;

const postalNoStr1 = '518000'
console.log(postalNoReg.test(postalNoStr1)) // true

const postalNoStr2 = '518000000'
console.log(postalNoReg.test(postalNoStr2)) // false

QQ号的校验(5-11位数字)

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

const qqStr1 = '1471059456';
console.log(qqReg.test(qqStr1)) // true

const qqStr2 = '1471059456456456456';
console.log(qqReg.test(qqStr2)) // false

微信号的校验

规则:(6至20位,以字母开头,字母,数字,减号,下划线)

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

const wxStr1 = 'weirui_888';
console.log(wxReg.test(wxStr1)) // true

const wxStr2 = '葳蕤888';
console.log(wxReg.test(wxStr2)) // false

车牌号的校验

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

const carNoStr1 = '粤A12345';
console.log(carNoReg.test(carNoStr1)) // true

const carNoStr2 = '深圳A12345';
console.log(carNoReg.test(carNoStr2)) // false

只含字母的字符串

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

const letterStr1 = 'weirui'
console.log(letterReg.test(letterStr1)) // true

const letterStr2 = 'weirui8888';
console.log(letterReg.test(letterStr2)) // false

含中文的字符串

const cnReg = /[\u4E00-\u9FA5]/;

const cnStr1 = '大家好,weirui'
console.log(cnReg.test(cnStr1)) // true

const cnStr2 = 'weirui'
console.log(cnReg.test(cnStr2)) // false

密码校验

规则:密码中必须包含字母、数字、特称字符,至少8个字符,最多30个字符

const passwordReg = /(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{8,30}/

const password1 = 'weirui#888..'
console.log(passwordReg.test(password1)) // true

const password2 = 'weirui9999999'
console.log(passwordReg.test(password2)) // false

字符串长度n的校验

function checkStrLength(n) { 
  return new RegExp(`^.{${n}}$`)
}
// 校验长度为10的字符串

const lengthReg = checkStrLength(10);

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

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