如何否定反向引用正则表达式

How to negate backreference regex

我正在制作一个正则表达式来验证具有以下必要条件的密码:

Have at least 6 characters.
Only have alphanumeric characters.
Don't have the same initial and ending character.

我想过让第一个和最后一个字符匹配,然后我会否定反向引用。我的问题在于如何否定反向引用。我在网上找了一些东西,但没有任何效果。这是我到目前为止得到的:

([\w])[\w]{3}[\w]+ //Generates a password with at least 6 chars in which the first and final characters match

使用这个模式

^(?=[0-9-a-zA-Z]+$)(.).{4,}(?!). 

Demo

您可以使用这个正则表达式:

^([0-9a-zA-Z])(?!.*$)[0-9a-zA-Z]{5,}$

RegEx Demo

  • (?!.*$) 将确保第一个和最后一个字符不相同。
  • [0-9a-zA-Z]{5,} 将确保长度至少为 6,并且输入中只有字母数字字符。