I want to ignore square brackets when using javascript regex

I use 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 will add later, If you want to). My code is:

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

It works great and excludes letters that I don’t want to enter to the user, except for square brackets and carriage characters. Of all the javascript regular expression tutorials I read, they are special characters - brackets mean any character between them and the carriage in this instance, meaning any character not between the square brackets. I searched here and on Google for an explanation of why these characters are also accepted, but cannot find an explanation.

So can someone help why my input accepts square brackets and a carriage?

+4
source share
4 answers

The reason is that you are using Az, not A-Za-z. The ascii range between Z (0x5a) and (0x61) includes square brackets, carriage, backtick, and underscore.

+7
source

Your regex does not match what you said:

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

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

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

Notes:

  • Az also includes characters:

      [\] ^ _ ` 
    .

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

     var pattern = /^[a-z0-9 "!&,'\\-]+$/i; 
  • \- is just a - character, because the backslash will act as a special character to escape. Use \\ to enable backslash.

  • ^ and $ are anchors used to match the beginning and end of a line. This ensures that the entire string is matched with the regular expression.

  • + used after a character class matches more than one character.


If you mean that you want to match characters other than those you accept and use this to prevent the user from entering β€œforbidden” characters, then the first note above describes your problem. Use A-Za-z instead of Az (the second note is also relevant).

+3
source

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

He tries to find a single character not A-z0-9 "!&,'\- ( ^ means not ).

Also, I'm not even sure what Az matches. This is either Az or Az .

So, your current regular expression matches strings like "." and "Hi." but not "Hi"

0
source

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

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

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

I think the problem with your regex is that Az is understood as all characters between 0x41 (65) and 0x7A (122), which include the characters [] ^ _ `that are between AZ and az. (Z - 0x5A (90), a - 0x61 (97), which means that the previous characters occupy 0x5B to 0x60).

0
source

Source: https://habr.com/ru/post/1500959/


All Articles