简体   繁体   中英

I want to ignore square brackets when using javascript regex

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:

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.

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

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

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. Az also includesthe characters:

     [\\]^_`  
    .

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

     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 Az (the second note is also relevant).

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 ).

Also, I'm not even sure what Az matches. It's either az or AZ .

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

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM