I'm building a password strength validator that check whether password contains lower-case and upper-case characters. I use regular expressions for that and get unexpected results when provided password string is undefined - see screenshot below. I would expect both checks to return false, yet the first one returns true.
Asked
Active
Viewed 335 times
2
-
maybe you are passing "undefined" as a string, rather than an actual undefined value? It is unfortunately not possible to tell this from your screenshot, but it would explain the output. – Nablezen Mar 30 '18 at 17:09
-
1@MFreidank you can clearly see what is passed to test() in the screenshot - nothing – jedrzej.kurylo Mar 30 '18 at 17:11
-
[Reference - Password Validation](https://stackoverflow.com/questions/48345922/reference-password-validation/) may help you out here. – ctwheels Apr 02 '18 at 13:48
1 Answers
4
Javascript will attempt to convert the argument of test
to a string if it's not one. So since:
String(undefined) === "undefined"
Your first regex is true since "undefined"
contains one or more lowercase letters. The second is false since there are no uppercase letters.
You can even verify this by noting that
/^undefined$/.test()
returns true
.

CRice
- 29,968
- 4
- 57
- 70