How word character is interpreted in character class?

后端 未结 2 766
感动是毒
感动是毒 2021-01-15 05:11

\\w - stands for [A-Za-z0-9_] Character class

But i am not able to understand how it is interpreted inside character class.

So when

2条回答
  •  南方客
    南方客 (楼主)
    2021-01-15 05:39

    I guess range is doing everything here, when you use ^[A-Za-z0-9_-~]+$

    _-~ matches a single character in the range between _ (index 95) and ~ (index 126) (case sensitive) that's why T| get matched and returns true but when you use ^[\w-~]+$ it is not forming any range of characters like _-~ to match so it fails and returns false

    See ^[A-Za-z0-9-~]+$ also returns false because it doesn't include the _ character to make the range _-~ between _ (index 95) and ~ (index 126)

    let test = (str) => /^[A-Za-z0-9-~]+$/.test(str)
    
    console.log(test("T|"))

    See https://regex101.com/r/vbLN9L/5 here on the EXPLANATION section.

提交回复
热议问题