Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· openaiHugging Face Suzhen/CodeChat

密码至少包含数字、大写字母、小写字母、特殊字符 3种以上(不包含中文及空格)正则

  1. User

    密码至少包含数字、大写字母、小写字母、特殊字符 3种以上(不包含中文及空格)正则

  2. Assistant

    表达式:

    ^(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[\W_])[^\u4e00-\u9fa5\s]{8,16}$
    

    解释:

    • ^ 表示字符串开头
    • (?=.*\d) 表示字符串中必须含有至少一个数字
    • (?=.*[A-Z]) 表示字符串中必须含有至少一个大写字母
    • (?=.*[a-z]) 表示字符串中必须含有至少一个小写字母
    • (?=.*[\W_]) 表示字符串中必须含有至少一个特殊字符(非单词字符),包括但不限于 !@#$%^&*()_+-={}[]|\:;"'<>,.?/
    • [^\u4e00-\u9fa5\s] 表示字符串中不能含有中文和空格
    • {8,16} 表示密码长度必须在 8-16 个字符之间
    • $ 表示字符串结尾

    此正则表达式适用于包含数字、大写字母、小写字母、特殊字符 3 种以上,且长度在 8 到 16 个字符之间的密码。

  3. User

    生成一个配置上面正则的密码

  4. Assistant

    随机生成一个符合密码强度要求的密码可以使用下面的 JavaScript 代码:

    // 随机生成符号
    function randomSymbol() {
      const symbols = "!@#$%^&*()_+-={}[]|\\:;\"'<>,.?/"; // 符号字符集
      return symbols[Math.floor(Math.random() * symbols.length)]; // 随机选择一个符号
    }
    
    // 生成密码
    function generatePassword() {
      let password = "";
      const passwordLength = Math.floor(Math.random() * 9) + 8; // 生成 8 到 16 个字符之间的随机长度
      const categories = [
        "0123456789", // 数字
        "abcdefghijklmnopqrstuvwxyz", // 小写字母
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ", // 大写字母
        "!@#$%^&*()_+-={}[]|\\:;\"'<>,.?/" // 特殊字符
      ]; // 符合要求的字符集
    
      // 随机选取 3 种不同的字符
      const selectedCategories = [];
      while (selectedCategories.length < 3) {
        const idx = Math.floor(Math.random() * categories.length);
        if (!selectedCategories.includes(idx)) {
          selectedCategories.push(idx);
        }
      }
    
      // 生成密码
      for (let i = 0; i < passwordLength; i++) {
        const idx = selectedCategories[Math.floor(Math.random() * selectedCategories.length)];
        const category = categories[idx];
        password += category[Math.floor(Math.random() * category.length)];
      }
    
      // 添加符号
      password += randomSymbol();
    
      return password;
    }
    
    console.log(generatePassword());
    

    这段代码会生成一个符合密码强度要求的随机密码,并且在密码结尾加上一个随机符号。每次运行代码会生成不同的密码。