Regex不允许某些特殊字符

时间:2021-04-08 11:00:42

I have the following regex which does not allow certain special characters:

我有以下不允许某些特殊字符的regex:

if (testString.match(/[`~,.<>;':"\/\[\]\|{}()-=_+]/)){    
    alert("password not valid");
}
else
{
    alert("password valid");
}

This is working. This regex will accept a password if it does not contain any of the special characters inside the bracket (~,.<>;':"\/\[\]\|{}()-=_+).

这是工作。如果不包含括号内的任何特殊字符(~,.<>;':"\/\[\]\|{}()-=_+),则此regex将接受密码。

My problem here is it also don't allow me to input numbers which is weird.

我的问题是它也不允许我输入奇怪的数字。

Anything I missed here? Thanks in advance!

什么我错过了的吗?提前谢谢!

Here is a sample:

这是一个示例:

jsFiddle

jsFiddle

1 个解决方案

#1


10  

You've got a character range in there: )-= which includes all ASCII characters between ) and = (including numbers). Move the - to the end of the class or escape it:

这里有一个字符范围:)-=,它包含)和=(包括数字)之间的所有ASCII字符。把-移到课程末尾,或者转义:

/[`~,.<>;':"\/\[\]\|{}()=_+-]/

Also, you don't need to escape all of those characters:

而且,你不需要把所有这些角色都转义:

/[`~,.<>;':"/[\]|{}()=_+-]/

Note that in your case, it is probably enough for you, to use test instead of match:

注意,在你的情况下,使用测试而不是匹配可能就足够了:

if (/[`~,.<>;':"/[\]|{}()=_+-]/.test(testString))){
    ...

test returns a boolean (which is all you need), while match returns an array with all capturing groups (which you are discarding anyway).

test返回一个布尔值(这是您所需要的),而match返回一个包含所有捕获组的数组(无论如何您都要丢弃它们)。

Note that, as Daren Thomas points out in a comment, you should rather decide which characters you want to allow. Because the current approach doesn't take care of all sorts of weird Unicode characters, while complaining about some fairly standard ones like _. To create a whitelist, you can simply invert both the character class and the condition:

请注意,正如Daren Thomas在评论中指出的,您应该选择允许哪些字符。因为当前的方法不考虑各种奇怪的Unicode字符,而抱怨一些相当标准的字符,比如_。要创建一个白名单,您可以简单地颠倒字符类和条件:

if (!/[^a-zA-Z0-9]/.test(testString)) {
   ...

And include all the characters you do want to allow.

并包括所有你想允许的字符。

#1


10  

You've got a character range in there: )-= which includes all ASCII characters between ) and = (including numbers). Move the - to the end of the class or escape it:

这里有一个字符范围:)-=,它包含)和=(包括数字)之间的所有ASCII字符。把-移到课程末尾,或者转义:

/[`~,.<>;':"\/\[\]\|{}()=_+-]/

Also, you don't need to escape all of those characters:

而且,你不需要把所有这些角色都转义:

/[`~,.<>;':"/[\]|{}()=_+-]/

Note that in your case, it is probably enough for you, to use test instead of match:

注意,在你的情况下,使用测试而不是匹配可能就足够了:

if (/[`~,.<>;':"/[\]|{}()=_+-]/.test(testString))){
    ...

test returns a boolean (which is all you need), while match returns an array with all capturing groups (which you are discarding anyway).

test返回一个布尔值(这是您所需要的),而match返回一个包含所有捕获组的数组(无论如何您都要丢弃它们)。

Note that, as Daren Thomas points out in a comment, you should rather decide which characters you want to allow. Because the current approach doesn't take care of all sorts of weird Unicode characters, while complaining about some fairly standard ones like _. To create a whitelist, you can simply invert both the character class and the condition:

请注意,正如Daren Thomas在评论中指出的,您应该选择允许哪些字符。因为当前的方法不考虑各种奇怪的Unicode字符,而抱怨一些相当标准的字符,比如_。要创建一个白名单,您可以简单地颠倒字符类和条件:

if (!/[^a-zA-Z0-9]/.test(testString)) {
   ...

And include all the characters you do want to allow.

并包括所有你想允许的字符。