I want to ignore square brackets when using javascript regex

前端 未结 4 1464
我在风中等你
我在风中等你 2021-01-13 13:08

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 followi

4条回答
  •  长发绾君心
    2021-01-13 13:44

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

提交回复
热议问题