站内搜索:
首页 >> 前端 >> 内容
前端常用的正则表达式(代码实例)

时间:2018/3/24 10:48:14

匹配 正整数 || null

function checkNumber(theObj) {
  var reg = /^[1-9]\d*$|null/;
  if (reg.test(theObj)) {
    return true;
  }
  return false;
}

匹配 整形 || 小数 || null

function checkNumber(theObj) {
  var reg = /[1-9]\d*.\d*|0.\d*[1-9]\d*|null/;
  if (reg.test(theObj)) {
    return true;
  }
  return false;
}

匹配 国内手机号码

function checkNumber(theObj) {
  var reg = /0?(13|14|15|17|18|19)[0-9]{9}/;
  if (reg.test(theObj)) {
    return true;
  }
  return false;
}

匹配 身份证号码

function checkNumber(theObj) {
  var reg = /\d{17}[\d|x]|\d{15}/;
  if (reg.test(theObj)) {
    return true;
  }
  return false;
}

匹配 邮箱地址

function checkNumber(theObj) {
  var reg = /\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}/;
  if (reg.test(theObj)) {
    return true;
  }
  return false;
}

匹配 匹配用户名

function checkNumber(theObj) {
  var reg = /[A-Za-z0-9_\-\u4e00-\u9fa5]+/;
  if (reg.test(theObj)) {
    return true;
  }
  return false;
}

  • 上一篇:HTML基本指令讲解:格式框架、标题、横线、文字、有序表与无序表
  • 下一篇:杨辉三角 让你输出三角形的全部元素,以数组的方式(题解)
  • 返回顶部