正则表达式模式 - 什么是 ((?=.*\d)|(?=.*\W+)) 和 (?![.\n])

标签 regex

有人可以向我解释这个正则表达式模式吗?

在下面

(?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$

究竟是什么
((?=.*\d)|(?=.*\W+))

&
(?![.\n])

谢谢你

最佳答案

这些都是先行断言(正面和负面),以确保以下文本遵守某些规则,而无需实际捕获文本。

               # assert that
(?=^.{8,}$)    # there are at least 8 characters
(              # and
  (?=.*\d)       # there is at least a digit
  |              # or
  (?=.*\W+)      # there is one or more "non word" characters (\W is equivalent to [^a-zA-Z0-9_])
)              # and
(?![.\n])      # there is no . or newline and
(?=.*[A-Z])    # there is at least an upper case letter and
(?=.*[a-z]).*$ # there is at least a lower case letter
.*$            # in a string of any characters
(?! ... )是负前瞻的语法(如果没有...则匹配),(?= ... )用于积极的前瞻(如果有......则匹配)。这看起来很像密码验证!

关于正则表达式模式 - 什么是 ((?=.*\d)|(?=.*\W+)) 和 (?![.\n]),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21632713/

相关文章:

javascript - 使用 jQuery 在 <li> 中包装文本

regex - 引用-此正则表达式是什么意思?

c# - 如何获取某些 html 的所有图像 src

ios - 验证 iOS 的 url 以 www 开头

c - 用于用户名验证的正则表达式修改

javascript - 替换所有包含在 "or ' 中的字符串

javascript - 如何在javascript中将包含多个重复关键字的字符串拆分为数组?

ruby - 如何替换 ruby​​ 中模式的每个实例?

regex - Notepad++复制/粘贴每行的一部分并替换

javascript - 单词边界与 Javascript 中的开头或结尾不匹配