Skip to content

Instantly share code, notes, and snippets.

@skinnyfads
Created January 15, 2023 07:52
Show Gist options
  • Save skinnyfads/b1a799a38457aa44f7cb730b381c6a5d to your computer and use it in GitHub Desktop.
Save skinnyfads/b1a799a38457aa44f7cb730b381c6a5d to your computer and use it in GitHub Desktop.
Check if a string is valid MongoDB ObjectId
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