Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检查密码时发生正则表达式错误_Javascript_Python_Regex - Fatal编程技术网

Javascript 检查密码时发生正则表达式错误

Javascript 检查密码时发生正则表达式错误,javascript,python,regex,Javascript,Python,Regex,在javascript中,我试图检查密码的长度必须至少为8个字符,并且必须至少包含一个字母、一个数字,并且除了@.-,没有其他特殊符号。 为此,我使用这个正则表达式 ^(?=(.*\d){1})(?=.*[a-zA-Z])(?=.*[!@#$%])[0-9a-zA-Z_@.-]{8,} 但是当我试着匹配一个字符串时 ^(?=(.*\d){1})(?=.*[a-zA-Z])(?=.*[!@#$%])[0-9a-zA-Z_@.-]{8,}.test('password') 它给出了syntex错

在javascript中,我试图检查密码的长度必须至少为8个字符,并且必须至少包含一个字母、一个数字,并且除了@.-,没有其他特殊符号。 为此,我使用这个正则表达式

^(?=(.*\d){1})(?=.*[a-zA-Z])(?=.*[!@#$%])[0-9a-zA-Z_@.-]{8,}
但是当我试着匹配一个字符串时

^(?=(.*\d){1})(?=.*[a-zA-Z])(?=.*[!@#$%])[0-9a-zA-Z_@.-]{8,}.test('password')
它给出了syntex错误

SyntaxError: expected expression, got '^'

我还必须在python中检查同样的内容。

JS正则表达式文本需要包装在
/
中。
/

/^(?=(.*\d){1})(?=.*[a-zA-Z])(?=.*[!@#$%])[0-9a-zA-Z_@.-]{8,}/.test('password')

如果要在不带正则表达式的Python中检查这些条件:

def check_password(password):
    return len(password) > 7 and any(character.isalpha() for character in password) and any(character.isdigit() for character in password) and all(character.isalnum() or character in '_@.-' for character in password)
或者:

def check_password(password):
    if len(password) < 8: return False
    alpha = digit = False
    nosymbol = True
    for character in password:
        if character.isalpha():
            alpha = True
        if character.isdigit():
            digit = True
        if not (character.isalnum() or character in '_@.-'):
            nosymbol = False
    return alpha and digit and nosymbol
def检查密码(密码):
如果len(密码)<8:返回False
α=数字=假
nosymbol=True
对于密码中的字符:
如果character.isalpha():
阿尔法=真
如果字符.isdigit():
数字=真
如果不是(character.isalnum()或“@.-”中的字符):
nosymbol=False
返回字母、数字和nosymbol

您忘了告诉JS这是一个常规表达式,为什么不使用基本字符串方法呢?我相信JS有一套合理的。在Python中,您将使用
re.search()
re.match()
:复制答案后也会出现相同的错误:console.log(/^(?=(.*\d){1})(?=.[a-zA-Z])(?=.[!@$%][0-9a-zA-Z.-{8,}/.test(“密码”)在您的密码版本中,在
之前有两个额外的、不可见的字符-Unicode#8204和#8203。这些是“零宽度空间”和“零宽度非接合器”,这就是为什么您看不到它们的原因。显然,他们在这里是不合法的。