我需要验证字母字符(在请求中,node.js)和我使用的是:
/[a-zA-Z]+/.test(input)
Run Code Online (Sandbox Code Playgroud)
但是当传递值null时,它被验证OK(regexp返回TRUE)示例:
/[a-zA-Z]+/.test(null) // <--- this returns true :(
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下吗?我需要避免空值,谢谢!
该
test()方法执行搜索正则表达式和指定字符串之间的匹配.返回true或false
RegExp.prototype.test() 转换传递给a的参数 String
例如/\d/.test(0)也将回归true,符合市场预期,其中0的一个Number作为参数传递.