Created
January 15, 2023 07:52
-
-
Save skinnyfads/b1a799a38457aa44f7cb730b381c6a5d to your computer and use it in GitHub Desktop.
Check if a string is valid MongoDB ObjectId
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function validObjectId(text: string) { | |
const pattern = /[0-9a-fA-F]{24}/; | |
const match = text.match(pattern); | |
if (!match) return false; | |
return match[0] === text; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment