JavaScript / ECMAScript

MongoDB ObjectId in JS

Match MongoDB ObjectId values — 24-character hexadecimal strings.

Try it in the JS tester →

Pattern

regexJS
\b[0-9a-fA-F]{24}\b   (flags: g)

JavaScript / ECMAScript code

jsJavaScript
const re = new RegExp("\\b[0-9a-fA-F]{24}\\b", "g");
const input = "507f1f77bcf86cd799439011";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));

Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).

How the pattern works

Word-bounded exactly 24 hex digits. Case-insensitive character class covers both upper and lower case forms produced by different drivers.

Examples

Input

507f1f77bcf86cd799439011

Matches

  • 507f1f77bcf86cd799439011

Input

id: 5f8d0d55b54764421b7156c4

Matches

  • 5f8d0d55b54764421b7156c4

Input

not an objectid

No match

Same pattern, other engines

← Back to MongoDB ObjectId overview (all engines)