Regex patterns/Validation
UUID (version 1–5)
The canonical 8-4-4-4-12 form, with the version and variant digits actually checked.
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/iThe problem
Check that a string is a canonically formatted UUID before using it as a key.
How it reads
Follow the line from left to right — every path you can trace is a string this pattern matches.
^[0-9a-f]{8}-[0-9a-f]…In order:^The start of the text (or of a line with the m flag)[0-9a-f]{8}Any one of: "0" to "9" or "a" to "f", exactly 8 times, as many as possible-The character "-"[0-9a-f]{4}Any one of: "0" to "9" or "a" to "f", exactly 4 times, as many as possible-The character "-"[1-5]Any one of: "1" to "5"[0-9a-f]{3}Any one of: "0" to "9" or "a" to "f", exactly 3 times, as many as possible-The character "-"[89ab]Any one of: "8", "9", "a" or "b"[0-9a-f]{3}Any one of: "0" to "9" or "a" to "f", exactly 3 times, as many as possible-The character "-"[0-9a-f]{12}Any one of: "0" to "9" or "a" to "f", exactly 12 times, as many as possible$The end of the text (or of a line with the m flag)
Matches
- 123e4567-e89b-12d3-a456-426614174000
- 123E4567-E89B-42D3-A456-426614174000
Does not match
- not-a-uuid
- 123e4567e89b12d3a456426614174000
- 123e4567-e89b-02d3-a456-426614174000
Where it bites
- The third group starts with [1-5] (the version) and the fourth with [89ab] (the variant). A nil UUID of all zeros is therefore rejected — add 0 to both sets if you need it.
- Without the i flag, uppercase UUIDs fail. They are valid.
- Matching the shape says nothing about whether the UUID exists in your database.