🔍Regex Recipes

UUID v4 Validation

Validate UUID version 4 format with strict formatting.

Pattern

^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$

Explanation

Validates UUID v4 format with the version bit (4) and variant bits (8, 9, a, or b) in correct positions.

Examples

Valid UUID v4
Input
123e4567-e89b-42d3-a456-426614174000
Output
✓ Match
Valid UUID v4
Input
a3bb189e-8bf9-4a56-9862-0def16a42540
Output
✓ Match
Invalid - wrong version
Input
123e4567-e89b-12d3-a456-426614174000
Output
✗ No match
Invalid - wrong format
Input
123e4567e89b42d3a456426614174000
Output
✗ No match

Code Examples

JavaScript
const uuidV4Regex = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
const isValidUUID = uuidV4Regex.test('a3bb189e-8bf9-4a56-9862-0def16a42540');
TypeScript
type UUID = string & { readonly __brand: unique symbol };

function isUUID(value: string): value is UUID {
  return /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(value);
}

Try it Now

💡 Tips

  • Use crypto.randomUUID() in modern browsers to generate
  • Add case-insensitive flag (i) to accept uppercase
  • Consider validating other UUID versions if needed

⚠️ Common Pitfalls

  • This pattern only validates UUID v4, not other versions
  • Case-insensitive flag needed for uppercase UUIDs
  • Does not validate UUID uniqueness or generation correctness