Marek Sebera Marek Sebera - 7 months ago 118
Javascript Question

How to test valid UUID/GUID?

How to check if variable contains valid UUID/GUID identifier ?

I'm currently interested only in validating types 1 and 4, but it's not limit for your answer.

Answer

Currently, UUID's are as specified in RFC4122.

Therefore to validate a UUID...

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

...ensures you have a canonically formatted UUID that is Version 1 through 5 and is the appropriate Variant as per RFC4122.

NOTE: Braces "{" and "}" are not canonical. They are an artefact of some systems and usages.

Easy to modify the above regex to meet the requirements of the original question.

HINT: regex group/captures