JS的常用验证正则

发布时间 2024-01-12 15:51:24作者: ajajaz

密码正则

密码必须为8到16位且必须包含数字和字母以及特殊字符

const pattern2 =/^(?!([A-Z]*|[a-z]*|[0-9]*|[!-/:-@\[-`{-~]*|[A-Za-z]*|[A-Z0-9]*|[A-Z!-/:-@\[-`{-~]*|[a-z0-9]*|[a-z!-/:-@\[-`{-~]*|[0-9!-/:-@\[-`{-~]*)$)[A-Za-z0-9!-/:-@\[-`{-~]{8,16}$/;

判断给定字符串中是否存在连续的三个字符(字母或数字)

function hasConsecutiveOrder(str) {
  // 循环遍历字符串中的字符
  for (var i = 1; i < str.length - 1; i++) {
    let currentCharCode = str.charCodeAt(i); // 当前字符的 ASCII 码值
    let previousCharCode = str.charCodeAt(i - 1); // 前一个字符的 ASCII 码值
    let nextCharCode = str.charCodeAt(i + 1); // 后一个字符的 ASCII 码值// 如果当前字符与前一个字符相差 1,同时与后一个字符相差 -1,表示存在连续的三个字符
    if (
      currentCharCode === previousCharCode + 1 &&
      currentCharCode === nextCharCode - 1
    ) {
      return false; // 返回 false,表示校验未通过
    }
  }
  return true; // 循环结束后,返回 true,表示校验通过
}

判断是否连续相同数字或字母

    // 判断是否连续相同数字或字母
    function lxSameStr (password) {
      const reg = /(\w)*(\w)\2{2}(\w)*/g
      if (reg.test(password)) {
        return false
      } else {
        return true
      }
	 }

密码强度正则

强:字母+数字+特殊字符 
 ^(?![a-zA-z]+$)(?!\d+$)(?![!@#$%^&*]+$)(?![a-zA-z\d]+$)(?![a-zA-z!@#$%^&*]+$)(?![\d!@#$%^&*]+$)[a-zA-Z\d!@#$%^&*]+$
   
    
中:字母+数字,字母+特殊字符,数字+特殊字符
     ^(?![a-zA-z]+$)(?!\d+$)(?![!@#$%^&*]+$)[a-zA-Z\d!@#$%^&*]+$

弱:纯数字,纯字母,纯特殊字符
^(?:\d+|[a-zA-Z]+|[!@#$%^&*]+)$

常用正则

^\\d+$  //非负整数(正整数 + 0)
^[0-9]*[1-9][0-9]*$  //正整数
^((-\\d+)|(0+))$  //非正整数(负整数 + 0)
^-[0-9]*[1-9][0-9]*$  //负整数
^-?\\d+$    //整数
^\\d+(  //非负浮点数(正浮点数 + 0)
^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$
//正浮点数
^((-\\d+(  //非正浮点数(负浮点数 + 0)
^(-(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*)))$
//负浮点数
^(-?\\d+)(  //浮点数
^[A-Za-z]+$  //由26个英文字母组成的字符串
^[A-Z]+$  //由26个英文字母的大写组成的字符串
^[a-z]+$  //由26个英文字母的小写组成的字符串
^[A-Za-z0-9]+$  //由数字和26个英文字母组成的字符串
^\\w+$  //由数字、26个英文字母或者下划线组成的字符串
^[\\w-]+(    //email地址
^[a-zA-z]+://(  //url

[1]*$


  1. A-Za-z0-9_ ↩︎