Code & Markup · In-Browser Regular Expression Lab
Git Commit Hash Regex — Short (7-Char) and Full (40-Char) Hashes
Matches Git commit SHA hashes: 7-character short commit IDs or 40-character full SHA-1 hashes.
Flags:
//i
Match Found
Load Sample:
Match Inspection & Details1 match
Match #1:7866615
Test Samples Verification Matrix
Expected Valid Samples (Should Match)
7866615 Pass
f21e094 Pass
4b825dc642cb6eb9a060e54bf8d69288fbee4904 Pass
Expected Invalid Samples (Must Reject)
12345 Rejected (Pass)
not-a-hash Rejected (Pass)
4b825dc642cb6eb9a060e54bf8d69288fbee4904z Rejected (Pass)
Code Implementation Generator
// JavaScript / TypeScript (ES6+)
const pattern = /^\b([a-f0-9]{7}|[a-f0-9]{40})$/i;
const text = "7866615";
const isMatch = pattern.test(text);
console.log("Is Match:", isMatch);
if (isMatch) {
const matches = text.match(pattern);
console.log("Matches:", matches);
}Regular Expression Token Breakdown
Matches standard Git short hashes (7 hex chars) or complete repository commit hashes (40 hex chars). Each component of the regular expression serves a distinct syntactical purpose as mapped below:
| Token Pattern | Name / Component | Function & Rule |
|---|---|---|
| [a-f0-9]{7} | Short Hash | Standard 7-char abbreviated Git commit. |
| [a-f0-9]{40} | Full SHA-1 | Full 40-char commit hash. |
Frequently Asked Questions
Does Git support SHA-256?
Related Regular Expressions
Browse All 40 PatternsAuthentication & Security
SHA-256 Hash Checksum
Matches 64-character hexadecimal SHA-256 cryptographic digest strings.
/^[a-fA-F0-9]{64}$/Test regex
Authentication & Security
MD5 Hash Checksum
Matches 32-character hexadecimal MD5 hash checksum strings.
/^[a-fA-F0-9]{32}$/Test regex
Code & Markup
Semantic Version (SemVer 2.0)
Full SemVer 2.0.0 specification regex validating major, minor, patch, and optional pre-release/build tags.
/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/Test regex