Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$

Matches a string representing a UUID (version 4) in its canonical 8-4-4-4-12 hexadecimal format, case-insensitive.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string Start of 'a1b2c3d4-5678-90ef-1234-567890abcdef'
[0-9a-fA-F] Character class: matches any single hexadecimal digit (0-9, a-f, A-F) 0, 9, a, f, A, F
{8} Quantifier: exactly 8 occurrences of the preceding token (hex digit) a1b2c3d4
- Literal hyphen character The hyphen in 'a1b2c3d4-5678...'
[0-9a-fA-F] Character class: matches any single hexadecimal digit (0-9, a-f, A-F) 5, 6, 7, 8
{4} Quantifier: exactly 4 occurrences of the preceding token (hex digit) 5678
- Literal hyphen character The hyphen in '...-5678-90ef...'
[0-9a-fA-F] Character class: matches any single hexadecimal digit (0-9, a-f, A-F) 9, 0, e, f
{4} Quantifier: exactly 4 occurrences of the preceding token (hex digit) 90ef
- Literal hyphen character The hyphen in '...-90ef-1234...'
[0-9a-fA-F] Character class: matches any single hexadecimal digit (0-9, a-f, A-F) 1, 2, 3, 4
{4} Quantifier: exactly 4 occurrences of the preceding token (hex digit) 1234
- Literal hyphen character The hyphen in '...-1234-567890abcdef'
[0-9a-fA-F] Character class: matches any single hexadecimal digit (0-9, a-f, A-F) 5, 6, 7, 8, 9, 0, a, b, c, d, e, f
{12} Quantifier: exactly 12 occurrences of the preceding token (hex digit) 567890abcdef
$ Anchor: asserts position at end of the string End of 'a1b2c3d4-5678-90ef-1234-567890abcdef'

Matches

  • 550e8400-e29b-41d4-a716-446655440000
  • A1B2C3D4-5678-90EF-1234-567890ABCDEF
  • 00000000-0000-0000-0000-000000000000
  • ffffffff-ffff-ffff-ffff-ffffffffffff

Does not match

  • 550e8400-e29b-41d4-a716-44665544000
  • 550e8400-e29b-41d4-a716-4466554400000
  • 550e8400-e29b41d4-a716-446655440000
  • 550e8400-e29b-41d4-a716-44665544000g
  • 550e8400-e29b-41d4-a716-446655440000-

Common pitfalls

  • Forgetting the anchors (^ and $) may match UUIDs embedded in longer strings.
  • Using [0-9a-f] without the A-F part misses uppercase hex digits.
  • Using [0-9a-f]+ instead of [0-9a-f]{n} may match invalid lengths.
  • Assuming this matches all UUID versions (it only matches version-agnostic hex format).
  • Not accounting for case sensitivity if the input may contain mixed case.

Performance notes

This pattern is linear in complexity and poses no risk of catastrophic backtracking. Anchors ensure full-string matching, preventing partial matches. For large inputs, consider using the 'i' flag if case insensitivity is desired (e.g., /^[0-9a-f]{8}...$/i).

About regular expressions

Regular expressions are tiny programs that match patterns in text. They're powerful and notoriously cryptic — most engineers can write them but few can read them confidently. Qaynaq Labs' regex explainer breaks any pattern into its constituent tokens, shows what each one does, and runs through both matching and non-matching examples so you can verify the logic.

Different regex flavors (JavaScript, PCRE, Python, Go) have subtle differences in syntax and supported features. Pick your flavor in the input — the explainer accounts for it.

Frequently asked

Does this pattern validate UUID version 4 specifically?

No. This pattern matches any UUID in the canonical 8-4-4-4-12 hexadecimal format regardless of version. To validate version 4 UUIDs, additional checks (e.g., version field in the third group) are required.

Can this pattern match UUIDs with braces like {550e8400-e29b-41d4-a716-446655440000}?

No. This pattern does not account for optional braces or other delimiters around the UUID.

Is the 'i' flag needed for case insensitivity?

No. The pattern already includes both lowercase and uppercase hex digits in the character class [0-9a-fA-F], so the 'i' flag is redundant here.

What if the input string has leading or trailing whitespace?

The anchors (^ and $) will prevent matching if there is any whitespace before or after the UUID. Use trim() on the input string or adjust the pattern to allow optional whitespace if needed.