我想在使用javascript正则表达式时忽略方括号

时间:2022-02-08 21:46:23

I am using javascript regex to do some data validation and specify the characters that i want to accept (I want to accept any alphanumeric characters, spaces and the following !&,'\- and maybe a few more that I'll add later if needed). My code is:

我正在使用javascript正则表达式进行一些数据验证并指定我想接受的字符(我想接受任何字母数字字符,空格和以下内容!&,'\ - 以及我可能会在以后添加的更多内容需要)。我的代码是:

var value = userInput;
var pattern = /[^A-z0-9 "!&,'\-]/;
if(patt.test(value) == true) then do something

It works fine and excludes the letters that I don't want the user to enter except the square bracket and the caret symbols. From all the javascript regex tutorials that i have read they are special characters - the brackets meaning any character between them and the caret in this instance meaning any character not in between the square brackets. I have searched here and on google for an explanation as to why these characters are also accepted but can't find an explanation.

它工作正常,并排除了我不希望用户输入的字母,除了方括号和插入符号。从我读过的所有javascript正则表达式教程中,它们都是特殊字符 - 括号表示它们之间的任何字符和此实例中的插入符号意味着任何不在方括号之间的字符。我在这里和谷歌搜索了解为什么这些字符也被接受但无法找到解释。

So can anyone help, why does my input accept the square brackets and the caret?

所以任何人都可以帮忙,为什么我的输入接受方括号和插入符?

4 个解决方案

#1


7  

The reason is that you are using A-z rather than A-Za-z. The ascii range between Z (0x5a) and a (0x61) includes the square brackets, the caret, backquote, and underscore.

原因是你使用的是A-z而不是A-Za-z。 Z(0x5a)和a(0x61)之间的ascii范围包括方括号,插入符号,反引号和下划线。

#2


3  

Your regex is not in line with what you said:

你的正则表达式与你说的不一致:

I want to accept any alphanumeric characters, spaces and the following !&,'\- and maybe a few more that I'll add later if needed

我想接受任何字母数字字符,空格和以下内容!&,'\ - 以及如果需要我稍后会添加更多内容

If you want to accept only those characters, you need to remove the caret:

如果您只想接受这些字符,则需要删除插入符号:

var pattern = /^[A-Za-z0-9 "!&,'\\-]+$/;

Notes:

  1. A-z also includesthe characters:

    A-z还包括字符:

    [\]^_`
    .

    Use A-Za-z or use the i modifier to match only alphabets:

    使用A-Za-z或使用i修饰符仅匹配字母:

    var pattern = /^[a-z0-9 "!&,'\\-]+$/i;
    
  2. \- is only the character -, because the backslash will act as special character for escaping. Use \\ to allow a backslash.

    \ - 只是字符 - 因为反斜杠将作为转义的特殊字符。使用\\允许反斜杠。

  3. ^ and $ are anchors, used to match the beginning and end of the string. This ensures that the whole string is matched against the regex.

    ^和$是锚,用于匹配字符串的开头和结尾。这可以确保整个字符串与正则表达式匹配。

  4. + is used after the character class to match more than one character.

    +在字符类之后用于匹配多个字符。


If you mean that you want to match characters other than the ones you accept and are using this to prevent the user from entering 'forbidden' characters, then the first note above describes your issue. Use A-Za-z instead of A-z (the second note is also relevant).

如果您的意思是要匹配您接受的字符以外的字符并使用此字符来阻止用户输入“禁止”字符,则上面的第一个注释描述了您的问题。使用A-Za-z代替A-z(第二个音符也是相关的)。

#3


0  

I'm not sure what you want but I don't think your current regexp does what you think it does:

我不确定你想要什么,但我不认为你现在的正则表达式符合你的想法:

It tries to find one character is not A-z0-9 "!&,'\- (^ means not).

它试图找到一个字符不是A-z0-9“!&,'\ - (^表示不是)。

Also, I'm not even sure what A-z matches. It's either a-z or A-Z.

另外,我甚至不确定A-z匹配的是什么。它是a-z或A-Z。

So your current regexp matches strings like "." and "Hi." but not "Hi"

因此,您当前的正则表达式匹配“。”之类的字符串。和“你好。”但不是“嗨”

#4


0  

Try this: var pattern = /[^\w"!&,'\\-]/;

试试这个:var pattern = / [^ \ w“!&,'\\ - ] /;

Note: \w also includes _, so if you want to avoid that then try

注意:\ w还包含_,所以如果你想避免那么试试

var pattern = /[^a-z0-9"!&,'\\-]/i;

I think the issue with your regex is that A-z is being understood as all characters between 0x41 (65) and 0x7A (122), which included the characters []^_` that are between A-Z and a-z. (Z is 0x5A (90) and a is 0x61 (97), which means the preceding characters take up 0x5B thru 0x60).

我认为你的正则表达式的问题是A-z被理解为0x41(65)和0x7A(122)之间的所有字符,其中包括A-Z和a-z之间的字符[] ^ _。 (Z为0x5A(90),a为0x61(97),这意味着前面的字符占用0x5B到0x60)。

#1


7  

The reason is that you are using A-z rather than A-Za-z. The ascii range between Z (0x5a) and a (0x61) includes the square brackets, the caret, backquote, and underscore.

原因是你使用的是A-z而不是A-Za-z。 Z(0x5a)和a(0x61)之间的ascii范围包括方括号,插入符号,反引号和下划线。

#2


3  

Your regex is not in line with what you said:

你的正则表达式与你说的不一致:

I want to accept any alphanumeric characters, spaces and the following !&,'\- and maybe a few more that I'll add later if needed

我想接受任何字母数字字符,空格和以下内容!&,'\ - 以及如果需要我稍后会添加更多内容

If you want to accept only those characters, you need to remove the caret:

如果您只想接受这些字符,则需要删除插入符号:

var pattern = /^[A-Za-z0-9 "!&,'\\-]+$/;

Notes:

  1. A-z also includesthe characters:

    A-z还包括字符:

    [\]^_`
    .

    Use A-Za-z or use the i modifier to match only alphabets:

    使用A-Za-z或使用i修饰符仅匹配字母:

    var pattern = /^[a-z0-9 "!&,'\\-]+$/i;
    
  2. \- is only the character -, because the backslash will act as special character for escaping. Use \\ to allow a backslash.

    \ - 只是字符 - 因为反斜杠将作为转义的特殊字符。使用\\允许反斜杠。

  3. ^ and $ are anchors, used to match the beginning and end of the string. This ensures that the whole string is matched against the regex.

    ^和$是锚,用于匹配字符串的开头和结尾。这可以确保整个字符串与正则表达式匹配。

  4. + is used after the character class to match more than one character.

    +在字符类之后用于匹配多个字符。


If you mean that you want to match characters other than the ones you accept and are using this to prevent the user from entering 'forbidden' characters, then the first note above describes your issue. Use A-Za-z instead of A-z (the second note is also relevant).

如果您的意思是要匹配您接受的字符以外的字符并使用此字符来阻止用户输入“禁止”字符,则上面的第一个注释描述了您的问题。使用A-Za-z代替A-z(第二个音符也是相关的)。

#3


0  

I'm not sure what you want but I don't think your current regexp does what you think it does:

我不确定你想要什么,但我不认为你现在的正则表达式符合你的想法:

It tries to find one character is not A-z0-9 "!&,'\- (^ means not).

它试图找到一个字符不是A-z0-9“!&,'\ - (^表示不是)。

Also, I'm not even sure what A-z matches. It's either a-z or A-Z.

另外,我甚至不确定A-z匹配的是什么。它是a-z或A-Z。

So your current regexp matches strings like "." and "Hi." but not "Hi"

因此,您当前的正则表达式匹配“。”之类的字符串。和“你好。”但不是“嗨”

#4


0  

Try this: var pattern = /[^\w"!&,'\\-]/;

试试这个:var pattern = / [^ \ w“!&,'\\ - ] /;

Note: \w also includes _, so if you want to avoid that then try

注意:\ w还包含_,所以如果你想避免那么试试

var pattern = /[^a-z0-9"!&,'\\-]/i;

I think the issue with your regex is that A-z is being understood as all characters between 0x41 (65) and 0x7A (122), which included the characters []^_` that are between A-Z and a-z. (Z is 0x5A (90) and a is 0x61 (97), which means the preceding characters take up 0x5B thru 0x60).

我认为你的正则表达式的问题是A-z被理解为0x41(65)和0x7A(122)之间的所有字符,其中包括A-Z和a-z之间的字符[] ^ _。 (Z为0x5A(90),a为0x61(97),这意味着前面的字符占用0x5B到0x60)。