What Is the Regex for UUID Validation?
What is the regex for UUID validation? Here is the one most codebases actually need:
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
That is the whole answer for most people. It checks the shape: 32 hex digits in groups of 8, 4, 4, 4, and 12, hyphens in the right places, nothing else in the string. There is a stricter version that also pins the UUID version and variant, and I will get to it. But reach for the strict one only if you know why you need it, because it rejects UUIDs your system may legitimately produce.
The pattern, and what each piece checks
A UUID is 36 characters: the 8-4-4-4-12 hex groups plus four hyphens. The loose pattern mirrors that structure exactly. [0-9a-f]{8} matches the first group, then a literal hyphen, then {4} groups, and so on down to {12}. The ^ and $ anchors force the entire string to match, which matters more than people think: without them, a UUID-shaped substring buried in garbage passes the test. The i flag makes it case-insensitive, because UUIDs show up uppercase in plenty of real systems.
Why bother validating at all, instead of letting the database complain? Because a malformed UUID handed to Postgres raises an exception rather than returning no rows. A validation failure gets promoted into a server failure: a 500 where there should have been a clean rejection. Checking the shape before the query keeps that from happening. Five lines of defense at the API boundary, and the malformed ID never reaches the database.
The mistakes I see most: dropping the anchors, forgetting case-insensitivity, and reaching for \w instead of [0-9a-f]. That last one is sneaky. \w includes the underscore, which is not a hex digit, so it quietly accepts strings no UUID generator would ever emit.
The strict version, and when you actually need it
Now the delayed half of the answer. A version 4 UUID carries two extra markers baked into its digits: a literal 4 at the start of the third group (the version nibble, at string index 14) and one of 8, 9, a, or b at the start of the fourth group (the variant, at index 19). Pinning those gives the strict pattern:
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
A real v4 UUID like 180e423f-6c22-491c-b0ea-349eb60d6537 passes it: the third group starts with 4, the fourth with b. So when do you want this? When your system specifically mints v4 UUIDs and you want to catch anything else at the door. That is a narrower job than it sounds.
Here is my honest take: most codebases should use the loose pattern. The strict one rejects version 1 and version 7 UUIDs, which are perfectly valid identifiers. Libraries keep moving to v7, so a strict regex copied into a validator can start rejecting legitimate IDs the day a dependency updates. A loose pattern that matches exactly what your database tolerates is the right pattern. Save the strict one for the day you genuinely need to prove "this is a v4," not just "this is shaped like a UUID."
The one thing to keep
Default to the loose pattern with anchors and the case-insensitive flag. Add the version and variant pins only when you specifically need to prove v4. And run the check at the API boundary, before the malformed ID can become someone's 500.
Frequently asked questions
What does a UUID look like?
36 characters: 32 hexadecimal digits arranged in groups of 8, 4, 4, 4, and 12, separated by hyphens. Example: 180e423f-6c22-491c-b0ea-349eb60d6537.
Why do I need the ^ and $ anchors in a UUID regex?
Without anchors, the pattern matches a UUID-shaped substring inside a longer string, so surrounding garbage would pass. Anchors force the entire string to be the UUID.
Should the UUID regex be case-insensitive?
Yes. UUIDs appear in both lowercase and uppercase in the wild, so use the case-insensitive flag (i in JavaScript) or include A-F in the character class.
How do I validate specifically UUID version 4?
Pin the version nibble and the variant nibble: /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i. The literal 4 starts the third group and [89ab] starts the fourth.
Can I use \w instead of [0-9a-f] in a UUID regex?
No. \w includes the underscore, which is not a hexadecimal digit, so it would accept strings that are not valid UUIDs. Use [0-9a-f] with the case-insensitive flag.
Related reading: Regex for Hex Color Code Validation · IPv4 Address Regex Validation: the Pattern and Its Limits · Catastrophic Backtracking: The Regex That Took Down Cloudflare for 27 Minutes · Why Most Email Regex Patterns Are Wrong (And What to Use Instead)