Git Commit SHA in JS
Match Git commit hashes, both short (7 chars) and full (40 chars) forms.
Try it in the JS tester →Pattern
regexJS
\b[0-9a-f]{7,40}\b (flags: g)JavaScript / ECMAScript code
jsJavaScript
const re = new RegExp("\\b[0-9a-f]{7,40}\\b", "g");
const input = "commit 75d2cb0";
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
Hexadecimal string of 7 to 40 lowercase characters, word-bounded. Covers Git's abbreviated SHAs and full SHA-1 hashes.
Examples
Input
commit 75d2cb0Matches
75d2cb0
Input
e7827cc1234567890abcdef1234567890abcdef1Matches
e7827cc1234567890abcdef1234567890abcdef1
Input
zzzzzzzNo match
—