Regex for UUID
A UUID matches ^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$ with the i flag: five hex groups of 8, 4, 4, 4 and 12 characters. For version 4 only, require a 4 at the start of the third group and 8, 9, a or b at the start of the fourth.
Syntax
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/iExamples
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test("123e4567-e89b-12d3-a456-426614174000")Result: true
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test("9b2c4c1e-5f3a-4d2b-9e8f-1a2b3c4d5e6f")Result: true
Version 4: third group starts with 4, fourth with 8, 9, a or b.
/^[0-9a-f]{8}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{12}$/i.test("123e4567e89b12d3a456426614174000")Result: true
Optional dashes accept the 32 character compact form.
"/api/users/123e4567-e89b-12d3-a456-426614174000/orders".match(/[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}/i)[0]Result: "123e4567-e89b-12d3-a456-426614174000"
Common mistakes
- Forgetting the i flag, so uppercase GUIDs from .NET or SQL Server fail.
- Using \w{8}-\w{4}-... which accepts g to z and underscores; use [0-9a-f].
- Accepting any version when the application only issues v4; check the version digit when it matters.
- Matching without anchors or boundaries and accepting a UUID glued to other text; use ^ and $ or \b.
Practice Regex for UUID
These drills check your answer on live data, free, no account needed.
- Counted repeats: 3 to 5 digitsThe whole line must match. Match lines made of 3, 4 or 5 digits and nothing else.
- Ranges: a letter and two digitsThe whole line must match. Match one lowercase letter followed by exactly two digits, like a12.
- SKU codesThe whole line must match. A SKU is three capital letters, a hyphen and four digits, optionally followed by a hyphen and a size: XS, S, M, L or XL.
Questions
Is a GUID regex the same as a UUID regex?
Yes. GUID is the Microsoft name for the same 128-bit format. Microsoft tools often print them in uppercase or wrapped in braces, so use the i flag and strip the braces first.
What is the regex for a UUID v7?
Same as v4 but the third group starts with 7: ^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$ with the i flag.
How do I generate a UUID in JavaScript?
crypto.randomUUID() returns a v4 UUID in browsers and Node 19+. There is no built-in validator, which is why the regex is useful.
Related
- Regex for hex colorA hex color regex for 3 and 6 digit CSS codes, an extended one for alpha, pulling colors out of a stylesheet, and converting a match to RGB numbers.
- QuantifiersWhat * + ? {n} {n,} and {n,m} mean, how greedy matching works, how to repeat a group instead of a single character, and the silent {2, 3} mistake.
- Regex flagsWhat each JavaScript regex flag does (case-insensitive, global, multiline, dotAll, unicode, sticky), how to set them, and the lastIndex trap with g and test.
- Ranges in character classesHow character ranges work by code point, how to combine them, the invalid [a-Z] and surprising [A-z], and how to express numeric ranges with alternation.