简体   繁体   English

MongoDB ObjectID的正则表达式

[英]Regex for MongoDB ObjectID

With reference to this SO question, I have a scenario where I only need to match a hex string with af included. 参考这个 SO问题,我有一个场景,我只需要匹配包含af的十六进制字符串。 All else should not match. 其他一切都不匹配。 Example: 例:

checkForHexRegExp.test("112345679065574883030833"); // => false
checkForHexRegExp.test("FFFFFFFFFFFFFFFFFFFFFFFF"); // => false
checkForHexRegExp.test("45cbc4a0e4123f6920000002"); // => true

My use case is that I am working with a set of hex strings and would like to only validate as true those that are mongodb objectIDs. 我的用例是我正在使用一组十六进制字符串,并且只想验证那些是mongodb objectID的真实。

You can use following regular expression but it will not quite work 您可以使用以下正则表达式, 但它不会很有效

checkForHexRegExp = /^(?=[a-f\d]{24}$)(\d+[a-f]|[a-f]+\d)/i

Example: 例:

> checkForHexRegExp.test("112345679065574883030833")
false
> checkForHexRegExp.test("FFFFFFFFFFFFFFFFFFFFFFFF")
false
> checkForHexRegExp.test("45cbc4a0e4123f6920000002")
true

But, as I commented, 112345679065574883030833 , FFFFFFFFFFFFFFFFFFFFFFFF are also valid hexadecimal representations. 但是,正如我评论的那样, 112345679065574883030833FFFFFFFFFFFFFFFFFFFFFFFF也是有效的十六进制表示。

You should use /^[af\\d]{24}$/i because it passes all the above tests 你应该使用 /^[af\\d]{24}$/i因为它通过了所有上述测试

Technically, all examples in the question potentially could be valid ObjectIds. 从技术上讲,问题中的所有示例都可能是有效的ObjectIds。 If you have to add some additional verification and that regexp is not enough, then my suggestion is to check if the first 4 bytes are a valid timestamp. 如果你必须添加一些额外的验证并且regexp是不够的,那么我的建议是检查前4个字节是否是有效的时间戳。 You can even verify that ObjectId has been generated during a certain period of time (eg since your project has been started or so). 您甚至可以验证ObjectId是否在特定时间段内生成(例如,因为您的项目已经启动等)。 See ObjectId documentation for details. 有关详细信息,请参阅ObjectId文档

If there's another timestamp field in an object, then it's also possible to make sure that both times are really close. 如果对象中有另一个时间戳字段,那么也可以确保两个时间都非常接近。

Just for the reference, in MongoDB shell ObjectId::getTimestamp() method can be used to extract a timestamp from ObjectId. 仅供参考,在MongoDB shell中, ObjectId :: getTimestamp()方法可用于从ObjectId中提取时间戳。

I need a regex that will only match mongodb ObjectIDs 我需要一个只匹配mongodb ObjectID的正则表达式

If you need that, you will have to specify exactly what makes up a mongodb ObjectID, so that we can create the appropriate regex string for it. 如果需要,您必须准确指定构成mongodb ObjectID的内容,以便我们可以为它创建适当的正则表达式字符串。


This should technically work in js: 这应该在技术上适用于js:

var myregexp = /^[0-9a-fA-F]{24}$/;
subject = "112345679065574883030833";

if (subject.match(myregexp)) {
    // Successful match
} else {
    // Match attempt failed
}

I would do something like this 我会做这样的事情

function validateObjectId(id)
{
    var bool=false; 
    if(id.length==24) bool=/[a-f]+/.test(id);
    return bool;
}


> validateObjectId("112345679065574883030833")
  false
> validateObjectId("FFFFFFFFFFFFFFFFFFFFFFFF")
  false
> validateObjectId("45cbc4a0e4123f6920000002")
  true
> validateObjectId("45cbc4a0e4123f6920")
  false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM