Home >Web Front-end >JS Tutorial >Summary of commonly used regular expressions in javascript_javascript skills

Summary of commonly used regular expressions in javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:47:541178browse

Summary of commonly used regular expressions in JavaScript

/**
 * 检验各种规则
 * @param str 检验的内容
 * @param cType 预设的检验规则 字符串[
 *             empty,   检验是否为空
 *             telphone,  座机手机号码
 *             allphone,  所有手机号码
 *             ydphone,  移动手机号码
 *             ltphone,  联通手机号码
 *             dxphone,  电信手机号码
 *             email,   邮箱
 *             url,    网址
 *             cn,     汉字
 *             image,   图片格式
 *             emscode,  邮政编码
 *             isEmpty,  检查是否为空
 *             isint,    整数
 *             isfloat,  判断是否为正小数
 *             isnumber,  判断为实数
 *             words,   判断是否为英文字母
 *             wordsAndNum,      判断是否为字母+数字
 *             wordsAndNumAndDownline, 判断是否由数字、26个英文字母或者下划线组成的字符串
 *             qq,           QQ检验
 *             personCard18,      身份证18位
 *             personCard15,      身份证15位
 *             ]
 * @param regex 自定义表达式 传入格式例如:"^\-?[1-9]+\d*$"
 *
 * @description cType 与 regex 只能有一个为空
 *       如 checkObjectByRegex("测试中文", "cn");  // 判断中文
 *       如 checkObjectByRegex("测试中文", null, "^[\u4e00-\u9fa5]+$");  // 自定义表达式正则
 * @return {boolean}
 */
function checkObjectByRegex(str, cType, regex) {
  /**
   * 定义验证各种格式类型的正则表达式对象
   */
  var Regexs = {
    telphone: (/^((\(\d{3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}$/), //座机手机号码
    allphone: (/^((13[0-9])|(14[57])|(15[0-9])|(17[678])|(18[0-9]))[0-9]{8}$/),     //所有手机号码
    ydphone: (/^((13[4-9])|(15[012789])|147|178|(18[23478]))[0-9]{8}$/),      //移动手机号码
    ltphone: (/^((13[0-2])|(145)|(15[56])|(176)|(18[56]))[0-9]{8}$/),          //联通手机号码
    dxphone: (/^((133)|(153)|(177)|(180)|(181)|(189))[0-9]{8}$/),        //电信手机号码
    email: (/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/),//邮箱
    url: (/(?:https|http|ftp|rtsp|mms):\/\/.+\/[\w]+\.[\w]+/),         //网址
    cn: (/^[\u4e00-\u9fa5]+$/i),                        //汉字
    image: (/\.jpg$|\.jpeg$|\.png$/i),                         //图片格式
    emscode: (/^[1-9]\d{5}$/),                         //邮政编码
    isint: (/^(\-)?[1-9]+\d*$/),                        //整数
    isfloat: (/^[0-9]+\.?[0-9]*$/),                 //判断是否为正小数
    isnumber: (/^[-\+]?\d+(\.\d+)?$/),               //判断为实数
    words: (/^[A-Za-z]+$/),                     //判断是否为英文字母
    wordsAndNum: (/^[A-Za-z0-9]+$/),                //判断是否为字母+数字
    wordsAndNumAndDownline: (/^\w+$/),               //判断是否由数字、26个英文字母或者下划线组成的字符串
    qq: (/^[1-9]\d{4,11}$/),                    //QQ
    personCard18: (/^(\d{6})()?(\d{4})(\d{2})(\d{2})(\d{3})(\d|X)$/),  //身份证18位
    personCard15: (/^(\d{6})()?(\d{2})(\d{2})(\d{2})(\d{3})$/)     //身份证15位
  };
  var nReg;
  if (str == null || typeof(str) == "undefined") {
    str = "";
  }
  if (cType != null && typeof(cType) != "undefined") {
    if (cType == "isEmpty") {
      str = $.trim(str);
      if (str != null && typeof(str) != "undefined" && str != "") {
        return false;
      } else return true;
    }
    nReg = Regexs[cType];
    if (str == null || str == "") return false; //输入为空,认为是验证通过
    // 针对 18位身份证单独处理
    if (cType == 'personCard18') {
      var ary = str.match(Regexs[cType]);
      if (!(parseInt(ary[3]) >= 1900)) return false;
      var D = new Date(ary[3] + "/" + ary[4] + "/" + ary[5]);
      var isTrue = D.getFullYear() == ary[3] && (D.getMonth() + 1) == ary[4] && D.getDate() == ary[5];
      return isTrue;
    }
    // 针对 15位身份证单独处理
    if (cType == 'personCard15') {
      var ary = str.match(Regexs[cType]);
      var D = new Date("19" + ary[3] + "/" + ary[4] + "/" + ary[5]);
      var isTrue = D.getYear() == ary[3] && (D.getMonth() + 1) == ary[4] && D.getDate() == ary[5];
      return isTrue;
    }
  } else {
    // 自定义正则表达式处理
    if (regex != null && typeof(regex) != "undefined") {
      nReg = new RegExp(regex);
    } else {
      return false;
    }
  }
  return nReg.test(str);
}

The above is the entire content of this article, I hope you all like it.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn