如何使用 node 的驱动程序判断ObjectID是否有效
我试过:
var BSON = mongo.BSONPure;
console.log("Validity: " + BSON.ObjectID.isValid('ddsd'))
但我总是得到一个例外,而不是一个真或假.(异常只是一个'throw e;//process.nextTick error,或第一次勾选时的'error'事件'
如何使用 node 的驱动程序判断ObjectID是否有效
我试过:
var BSON = mongo.BSONPure;
console.log("Validity: " + BSON.ObjectID.isValid('ddsd'))
但我总是得到一个例外,而不是一个真或假.(异常只是一个'throw e;//process.nextTick error,或第一次勾选时的'error'事件'
This is a simple check-不是This is a simple check%万无一失
如果要判断包含24个十六进制字符的字符串,可以使用此正则表达式.
var checkForHexRegExp = new RegExp("^[0-9a-fA-F]{24}$")
checkForHexRegExp.test("i am a bad boy")
// false
checkForHexRegExp.test("5e63c3a5e4232e4cd0274ac2")
// true
正则表达式取自github.com/mongodb/js-bson/.../objectid.ts
为了更好地判断使用:
var ObjectID = require("mongodb").ObjectID
ObjectID.isValid("i am a bad boy")
// false
ObjectID.isValid("5e63c3a5e4232e4cd0274ac2")
// true