我需要验证字母字符(在请求中,node.js),我使用的是:
/[a-zA-Z]+/.test(input)
但是当传递 null 值时,它被验证为 OK(正则表达式返回 TRUE) 示例:
/[a-zA-Z]+/.test(null) // <--- this returns true :(
有人可以帮我解释一下吗?我需要避免空值,谢谢!
最佳答案
The
test()
method executes a search for a match between a regular expression and a specified string. Returns true or false
RegExp.prototype.test()
将传递的参数转换为String
例如 /\d/.test(0)
也会按预期返回 true
,其中 0
是 Number
作为参数传递。
关于javascript - 为什么这个 Javascript RegExp 返回 true?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38427177/