关于javascript:前端常用正则

6次阅读

共计 1223 个字符,预计需要花费 4 分钟才能阅读完成。

手机号

/** 手机号正则 (工信部 2019 年最新颁布)*/
const phoneRule = /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[189]))\d{8}$/;
 
/** 手机号校验 */
const phoneFun = (params) => {const regex = new RegExp(phoneRule);
    return regex.test(params)
}

身份证

/** 身份证正则 */
const cardIdRule = /(^\d{8}(0\d|10|11|12)([0-2]\d|30|31)\d{3}$)|(^\d{6}(18|19|20)\d{2}(0[1-9]|10|11|12)([0-2]\d|30|31)\d{3}(\d|X|x)$)/;
 
/** 身份证校验 */
const cardIdFun = (params) => {const regex = new RegExp(cardIdRule);
    return regex.test(params)
}

英文和数字

/** 英文和数字正则 */
const numEnsRule = /^[A-Za-z0-9]+$/;
 
/** 英文和数字校验 */
const numEnsRuleFun = (params) => {const regex = new RegExp(numEnsRule);
    return regex.test(params)
}

中文字符

/** 中文字符正则 */
const ChineseRule = /[\u4e00-\u9fa5]/;
 

/** 中文字符校验 */
const ChineseFun = (params) => {const regex = new RegExp(ChineseRule);
    return regex.test(params)
}

邮箱

/** 邮箱正则 */
const EmailRule = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
 
/** 邮箱校验 */
const EmailFun = (params) => {const regex = new RegExp(EmailRule);
    return regex.test(params)
}

中国邮政

/** 中国邮政正则 */
const postalRule = /[1-9]\d{5}(?!\d)/ ;
 
/** 中国邮政校验 */
const postalFun = (params) => {const regex = new RegExp(postalRule);
    return regex.test(params)
}

腾讯 QQ 号

 
/** 腾讯 QQ 号正则 */
const QQRule = /[1-9][0-9]{4,}/;
 
/** 腾讯 QQ 号校验 */
const QQFun = (params) => {const regex = new RegExp(QQRule);
    return regex.test(params)
}
正文完
 0