Validate.js ReGex 未按预期工作

问题描述

我正在尝试使用格式:模式:Validate.js 的功能来确保密码满足至少一个大写、一个小写、一个数字和一个特殊字符的要求,并且具有最小长度8 个,最多 20 个。存在和长度功能如预期。有人有任何提示吗?

 password: {
    presence: {
      allowEmpty: false,message: "is required",admin: "required",label: "Password*",},length: {
      minimum: 8,message: "must be at least 8 characters",maximum: 20,format: {
      pattern: /^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!|@|#|\$|%|\^|&|\*])(?=.{8,20})/,}
  },

解决方法

使用

/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*]).{8,20}$/

说明

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
    [a-z]                    any character of: 'a' to 'z'
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
    [A-Z]                    any character of: 'A' to 'Z'
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
    [0-9]                    any character of: '0' to '9'
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
    [!@#$%^&*]               any character of: '!','@','#','$','%','^','&','*'
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  .{8,20}                  any character except \n (between 8 and 20
                           times (matching the most amount possible))
--------------------------------------------------------------------------------
  $                        before an optional \n,and the end of the
                           string