2

I need to verify that an input is between 1 and 512 characters long. I'm using the standard length-check regex of /^.{1,512}$/. When I run

/^.{1,512}$/.test(null)

it returns true. How do I get a length-check regex to fail against null? And why does this test true against null in the first place?

EDIT: Leaving this here since it's more googleable in my case than the earlier question, but per here, the problem is that the regex coerces null into 'null' before testing.

4

1 回答 1

0

我会用这样的正则表达式进行测试:

var myregexp = /^(?!null).{1,512}$/m;;
于 2013-06-27T16:08:28.040 回答