Match UUIDs with regex
A regex that matches UUID v4 specifically (and a general one for any version) with the version + variant bits in the right places.
# pattern
/\b[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}\b/gi → Open in regex tester (pre-filled)
# how it works
UUID v4 has fixed bits: the 13th hex character is always 4 (the version), and the 17th must be 8, 9, a, or b (the variant). The pattern above enforces both. For any UUID version, replace `4` with `[1-5]` and `[89ab]` with `[89ab0-9]`.
# sample input
Real v4: 550e8400-e29b-41d4-a716-446655440000. Real v1: a8098c1a-f86e-11da-bd1a-00112444be1e. Fake: 00000000-0000-0000-0000-000000000000.
# pitfalls
- The all-zero UUID is a valid UUID format but is not a v4. The version-bit check correctly rejects it.
- UUIDs are case-insensitive — keep the /i flag on or use [0-9a-fA-F] explicitly.
- Some systems strip the dashes (32-char hex). Use a separate pattern: \b[0-9a-f]{32}\b.