Regex ['!@#$%*\]\[()-=_+{}:\";?,.\/A-Za-z0-9\s] is allowing < character

时间:2018-02-01 18:11:46

标签: javascript c# regex regex-group

I am using https://regex101.com/ to test my below regular expression but this expression is allowing < character which is not mentioned in the expression.

['!@#$%*\]\[()-=_+{}:\";?,.\/A-Za-z0-9\s]

1 个答案:

答案 0 :(得分:8)

- denotes a range inside a character class.

The range you're matching in your regex is all the characters that appear between ")" and "=", because:

['!@#$%*\]\[()-=_+{}:\";?,.\/A-Za-z0-9\s]
             ↑ ↑

And the "<" sign appears between them (see here):

enter image description here

You need to:

  • escape it, or
  • move it to the end (or beginning) of the class

Change to:

['!@#$%*\]\[()=_+{}:\";?,.\/A-Za-z0-9\s-]

Simpler example:

[1-9]

matches digits from "1" to "9", while:

[19-]

and

[1\-9]

matches "1", "9" and "-".