Skip to content Skip to sidebar Skip to footer

Regexp Gives Unexpected Result When Tested Against `undefined`

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

Solution 1:

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.

Post a Comment for "Regexp Gives Unexpected Result When Tested Against `undefined`"