Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does character range class [A-z] match underscore?

Tags:

regex

Tried this in many languages with same result.

JavaScript example:

/[A-z]/.test("_"); // => true
/[A-z]/.test("0"); // => false
/[A-z]/.test("-"); // => false
/[A-z]/.test("A"); // => true

Why is the first case not returing false?

like image 208
Jan Święcki Avatar asked Nov 09 '13 03:11

Jan Święcki


People also ask

Does underscore mean anything in regex?

The _ (underscore) character in the regular expression means that the zone name must have an underscore immediately following the alphanumeric string matched by the preceding brackets. The . (period) matches any character (a wildcard).

Is underscore a special character in regex?

regex allows only for the special character UNDERSCORE, SPACE, SINGLE QUOTE and HYPHEN.

What characters are allowed in this range AZ?

The range A-z is valid, but you should note that this includes non-letter characters like ^ and [ . 0-Z is also valid and includes : , ? , and a whole bunch of other characters you probably don't want. To answer your question, you can create any range in the right order.

What does AZ do in regex?

The regular expression [A-Z][a-z]* matches any sequence of letters that starts with an uppercase letter and is followed by zero or more lowercase letters. The special character * after the closing square bracket specifies to match zero or more occurrences of the character set.


1 Answers

Character ranges are not that intelligent. They are based on ascii codes. Check out Ascii Table. There exist special characters between upper-case A-Z and lower-case a-z range, namely:

[
\
]
^
_
`

So, instead of A-z it should be A-Za-z.

like image 123
Jan Święcki Avatar answered Sep 22 '22 20:09

Jan Święcki