这是我很困惑的javascript正则表达式。我知道(?=)是积极的前瞻性,但是在此之前是否应该有一个主表达式?
/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])\w{8,}$/
答案说它与密码匹配:
至少一个数字,一个小写字母和一个大写字母以及至少8个字母,数字或下划线字符
但是我不明白为什么。有人可以解释一下吗?
让我们分解一下:
/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])\w{8,}$/
^ // Match the start of the string
(?=.*\d) // Make sure the string contains at least one digit
(?=.*[a-z]) // Make sure the string contains at least one lowercase letter
(?=.*[A-Z]) // Make sure the string contains at least one uppercase letter
\w{8,} // Match at least eight word characters (alphanumeric or underscore)
$ // Match the end of the string
(?=.*PATTERN)
是确保匹配字符串包含的一种常见方式PATTERN
。
它的工作原理是.*
匹配任何内容(换行符除外);前瞻的字面意思是“这个正则表达式应该只在你找到PATTERN
之后才匹配。”
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句