a very simple & naive question: why this is true?
new RegExp('^[a-z]+$', 'i').test('B')
apparently 'B' is out of [a-z]?
Yes, but you have the i
parameter which tells the regex to ignore case.
From the MDN documentation for RegEx:
Parameters
pattern
The text of the regular expression.
flags
If specified, flags can have any combination of the following values:
...
i
ignore case
It's defining a class, which is to say [a-z]
is symbolic of "any character, from a
to z
."
Regex is, by nature, case SensAtiVe as well, so [a-z]
varies from [A-Z]
(unless you use the i
(case insensitive) flag, like you've demonstrated).
e.g.
/[a-z]/ -- Any single character, a through z
/[A-Z]/ -- Any single uppercase letter, A through Z
/[a-zA-Z]/ -- Any single upper or lowercase letter, a through z
/[a-z]/i or /[A-Z]/i -- (note the i) Any upper or lowercase letter, a through z
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With