1. 程式人生 > 其它 >前端常用的正則表示式(程式碼例項)

前端常用的正則表示式(程式碼例項)

  匹配 正整數 || 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_\-一-龥]+/;

  if (reg.test(theObj)) {

  return true;

  }

  return false;

  }