Query guide: JavaScript UUID regex

JavaScript regex UUID validator for regex.test checks

Use this page when a JavaScript expression needs to accept or reject UUID strings before a route handler, form field, import row, or API client continues. Test UUID v4 patterns, version bits, case handling, anchors, and failing samples online before copying the expression into regex.test().

Searches for javascript regex uuid validator, uuid regex javascript, and check regex javascript usually point to a boundary problem. The code needs to know whether a field is exactly a UUID, not whether a UUID-shaped substring appears somewhere inside a longer value.

The most common mistake is using a loose expression that checks length and hyphens but ignores UUID version and variant positions. That may be fine for extraction from logs, but a validator for IDs should be explicit about which formats the product accepts.

This workflow is JavaScript-first. It treats regex as a fast format screen, then leaves database existence, record ownership, authorization, and uniqueness to application code after the text shape is approved.

How to validate a UUID regex

  1. Decide whether the field accepts only UUID v4 values, UUID versions 1 through 5, lowercase canonical values, or compact 32-character hex IDs.
  2. Use `^` and `$` when `regex.test(value)` must approve the whole UUID string instead of a valid-looking fragment inside a larger value.
  3. Check the version nibble and variant nibble deliberately when the app expects UUID v4, not any hyphenated 128-bit identifier.
  4. Test uppercase input, missing hyphens, wrong group lengths, invalid hex characters, surrounding text, and legacy UUID versions before shipping.
  5. After regex passes, use application logic for uniqueness, database lookup, authorization, and any ID ownership rules.

UUID expressions to test

UUID v4 validator

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

Pass: 550e8400-e29b-41d4-a716-446655440000, 123e4567-e89b-42d3-a456-426614174000

Fail: 550e8400-e29b-11d4-a716-446655440000, not-a-uuid

Use this when generated identifiers must be version 4 UUIDs with the standard variant bits.

UUID versions 1 through 5

^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$

Pass: 550e8400-e29b-41d4-a716-446655440000, 550e8400-e29b-11d4-a716-446655440000

Fail: 550e8400-e29b-91d4-a716-446655440000, 550e8400-e29b-41d4-z716-446655440000

Good for APIs that accept older UUID versions but still require canonical hyphen positions and variant bits.

Lowercase canonical UUID v4

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

Pass: 550e8400-e29b-41d4-a716-446655440000

Fail: 550E8400-E29B-41D4-A716-446655440000, 550e8400e29b41d4a716446655440000

Use this when storage, routing, or cache keys must stay lowercase and canonical.

Compact 32-character UUID source

^[0-9a-fA-F]{32}$

Pass: 550e8400e29b41d4a716446655440000

Fail: 550e8400-e29b-41d4-a716-446655440000, 550e8400e29b41d4a71644665544

Only use this if your system intentionally accepts compact IDs and normalizes them before display or storage.

Pass and fail samples for UUID validators

Should pass UUID v4

550e8400-e29b-41d4-a716-446655440000, 123e4567-e89b-42d3-a456-426614174000

Should fail UUID v4

not-a-uuid, 550e8400e29b41d4a716446655440000, id=550e8400-e29b-41d4-a716-446655440000

Version decision

550e8400-e29b-11d4-a716-446655440000, 550e8400-e29b-51d4-a716-446655440000

Case decision

550E8400-E29B-41D4-A716-446655440000, 550e8400-e29b-41d4-a716-446655440000

Copy-ready JavaScript regex.test examples

Boolean UUID v4 regex.test check

const uuidV4Pattern = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/;
const isUuidV4 = uuidV4Pattern.test(value);

Use a non-global anchored regex for a single yes or no validator result.

Validate submitted ID rows

const uuidV4Pattern = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/;
const invalidIds = values.filter((value) => !uuidV4Pattern.test(value.trim()));

Trim only if the browser, API, and tests all use the same normalization rule.

Normalize casing after validation

const uuidV4Pattern = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/;
const canonicalId = uuidV4Pattern.test(value) ? value.toLowerCase() : null;

Regex approves the shape. Canonical casing should be an explicit application decision.

Avoid loose partial matches

const looseUuid = /[0-9a-f-]{36}/i;
looseUuid.test('id=550e8400-e29b-41d4-a716-446655440000'); // true, but the field is not only a UUID

Loose extraction patterns are useful for logs, but they are too permissive for a field validator.

UUID regex decisions to make first

  • Does the product require UUID v4 specifically, or can older UUID versions pass?
  • Should uppercase UUIDs pass, or should the UI normalize to lowercase before validation?
  • Are braces, URN prefixes, or compact 32-character UUIDs intentionally supported?
  • Will route params and API body fields use the exact same UUID expression?
  • Can the value include surrounding text, or must the whole field be only the UUID?
  • Do tests prove that invalid version, variant, length, and hex-character cases fail?

Test a UUID regex now

Open the live tester with a UUID v4 validator and mixed samples already loaded. The failing rows are included to expose missing hyphens, non-UUID text, wrong versions, and partial-match problems.

Test UUID Regex

Related Pages on Regex Tester

JavaScript UUID Regex FAQ

What is a JavaScript regex UUID validator?

It is an anchored regular expression used with the JavaScript RegExp engine to decide whether a string matches an accepted UUID format, such as canonical UUID v4.

Should a UUID validator check the version number?

Yes when your app expects a specific UUID version. For UUID v4, the third group should start with 4 and the variant group should start with 8, 9, a, or b.

Can regex prove a UUID exists in the database?

No. Regex can validate text shape and version bits, but existence, ownership, uniqueness, and authorization must be checked in application or database logic.

Should JavaScript UUID regex use the global flag with regex.test?

No for normal field validation. A global regex changes lastIndex between calls, so repeated `.test()` checks can become unreliable.

More Free Developer Tools