Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
是否有一种简单的内置方法来检查字符串是否是有效的 ObjectId?我知道如果我尝试从无效字符串构造 ObjectId 会引发异常,有没有办法绕过 try-catch 块?
一种可能的方法是检查这个正则表达式:^[0-9a-b]{48}$ 有没有更优雅的东西?
^[0-9a-b]{48}$
有方法ObjectId.isValid(String s):文档
ObjectId.isValid(String s)