Back to blog

UUID Regex Pattern: Validate UUID v4 in JavaScript

UUID v4 regex pattern

Use this pattern to validate a lowercase UUID v4 in the canonical 8-4-4-4-12 format:

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

Try it with the DevToolkit Regex Tester, or generate test data with the UUID Generator.

How the pattern works

PartMeaning
^Start of the input
[0-9a-f]{8}Eight hexadecimal characters
-A literal hyphen
[0-9a-f]{4}Four hexadecimal characters
4[0-9a-f]{3}Version 4 marker plus three hexadecimal characters
[89ab][0-9a-f]{3}RFC variant marker plus three hexadecimal characters
[0-9a-f]{12}Final twelve hexadecimal characters
$End of the input

The version and variant positions are why a generic hexadecimal pattern is not enough when you specifically need UUID v4.

JavaScript validation

const uuidV4 = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;

uuidV4.test("550e8400-e29b-41d4-a716-446655440000"); // true
uuidV4.test("550e8400-e29b-11d4-a716-446655440000"); // false

The i flag allows uppercase hexadecimal characters. The anchors ensure that extra characters before or after the UUID do not pass validation.

Regex validation is not enough

A matching string may still be duplicated, manually chosen, or generated with a weak random source. Use a database uniqueness constraint and a secure generator such as crypto.randomUUID() when creating identifiers. A UUID is also not a secret or access-control mechanism.

For more syntax examples, read the regex cheat sheet. For UUID format and version details, see what a UUID is.

Summary

Use a UUID v4-specific regex when you need to check canonical text format, version, and variant. Then enforce uniqueness and authorization separately in application code.