Javascript 如何测试有效的 UUID/GUID?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7905929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:00:38  来源:igfitidea点击:

How to test valid UUID/GUID?

javascriptregexvalidationuuidguid

提问by Marek Sebera

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

如何检查变量是否包含有效的 UUID/GUID 标识符?

I'm currently interested only in validating types 1 and 4, but it should not be a limitation to your answers.

我目前只对验证类型 1 和 4 感兴趣,但这不应限制您的答案。

回答by Gambol

Currently, UUID's are as specified in RFC4122. An often neglected edge case is the NIL UUID, noted here. The following regex takes this into account and will return a match for a NIL UUID. See below for a UUID which only accepts non-NIL UUIDs. Both of these solutions are for versions 1 to 5 (see the first character of the third block).

目前,UUID 是在 RFC4122 中指定的。一个经常被忽视的边缘情况是 NIL UUID,这里指出。以下正则表达式将这一点考虑在内,并将返回 NIL UUID 的匹配项。有关仅接受非 NIL UUID 的 UUID,请参见下文。这两种解决方案都适用于版本 1 到 5(参见第三个块的第一个字符)。

Therefore to validate a UUID...

因此要验证 UUID ...

/^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][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.

...确保您有一个规范格式的 UUID,它是版本 1 到版本 5,并且是符合 RFC4122 的相应变体。

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

注意:大括号{}不是规范的。它们是某些系统和用法的产物。

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

容易修改上面的正则表达式,以满足原题的要求。

HINT: regex group/captures

提示:正则表达式组/捕获

To avoid matching NIL UUID:

为了避免匹配 NIL 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

回答by ryanb

regex to the rescue

正则表达式来救援

/^[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}$/.test('01234567-9ABC-DEF0-1234-56789ABCDEF0');

or with brackets

或带括号

/^\{?[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}??\}?$/

回答by Ivan Gabriele

If you want to check or validate a specific UUID version, here are the corresponding regexes.

如果您想检查或验证特定的 UUID 版本,这里是相应的正则表达式。

Note that the only difference is the version number, which is explained in 4.1.3. Versionchapter of UUID 4122 RFC.

请注意,唯一的区别是版本号,这在UUID 4122 RFC 的4.1.3. Version章节中进行了解释。

The version number is the first character of the third group : [VERSION_NUMBER][0-9A-F]{3}:

版本号是第三组的第一个字符[VERSION_NUMBER][0-9A-F]{3}::

  • UUID v1 :

    /^[0-9A-F]{8}-[0-9A-F]{4}-[1][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
    
  • UUID v2 :

    /^[0-9A-F]{8}-[0-9A-F]{4}-[2][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
    
  • UUID v3 :

    /^[0-9A-F]{8}-[0-9A-F]{4}-[3][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
    
  • UUID v4 :

    /^[0-9A-F]{8}-[0-9A-F]{4}-[4][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
    
  • UUID v5 :

    /^[0-9A-F]{8}-[0-9A-F]{4}-[5][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
    
  • UUID v1 :

    /^[0-9A-F]{8}-[0-9A-F]{4}-[1][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
    
  • UUID v2 :

    /^[0-9A-F]{8}-[0-9A-F]{4}-[2][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
    
  • UUID v3 :

    /^[0-9A-F]{8}-[0-9A-F]{4}-[3][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
    
  • UUID v4 :

    /^[0-9A-F]{8}-[0-9A-F]{4}-[4][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
    
  • UUID v5:

    /^[0-9A-F]{8}-[0-9A-F]{4}-[5][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
    

回答by Neeraj Sharma

If you are using Node.js for development, it is recommended to use a package called Validator. It includes all the regexes required to validate different versions of UUID's plus you get various other functions for validation.

如果您使用 Node.js 进行开发,建议使用名为 Validator 的包。它包括验证不同版本的 UUID 所需的所有正则表达式,此外您还可以获得各种其他验证功能。

Here is the npm link: Validator

这是 npm 链接:验证器

var a = 'd3aa88e2-c754-41e0-8ba6-4198a34aa0a2'
v.isUUID(a)
true
v.isUUID('abc')
false
v.isNull(a)
false

回答by Wolf

Beside Gambol's answerthat will do the job in nearly all cases, all answers given so far missed that the grouped formatting (8-4-4-4-12) is not mandatory to encode GUIDs in text. It's used extremely often but obviously also a plain chain of 32 hexadecimal digits can be valid.[1]regexenh:

除了几乎在所有情况下都能完成这项工作的Gambol 答案之外,迄今为止给出的所有答案都没有指出分组格式 (8-4-4-4-12) 不是强制将GUID 编码为 text。它使用非常频繁,但显然 32 个十六进制数字的普通链也可以是有效的。[1]正则表达式ENH

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


[1]The question is about checking variables, so we should include the user-unfriendly form as well.

[1]所述的问题是关于校验荷兰国际集团变量S,所以我们应该包括用户不友好的形式为好。

回答by Evan Edwards

All type-specific regexes posted so far are failing on the "type 0" Nil UUID, defined in 4.1.7 of the RFC as:

到目前为止发布的所有特定于类型的正则表达式都在“类型 0”Nil UUID 上失败,在 RFC 的 4.1.7 中定义为:

The nil UUID is special form of UUID that is specified to have all 128 bits set to zero: 00000000-0000-0000-0000-000000000000

nil UUID 是 UUID 的特殊形式,它被指定为将所有 128 位设置为零: 00000000-0000-0000-0000-000000000000

To modify Wolf's answer:

修改 Wolf 的答案:

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

Or, to properly exclude a "type 0" without all zeros, we have the following (thanks to Luke):

或者,要正确排除没有全零的“类型 0”,我们有以下内容(感谢 Luke):

/^(?:[0-9a-f]{8}-?[0-9a-f]{4}-?[1-5][0-9a-f]{3}-?[89ab][0-9a??-f]{3}-?[0-9a-f]{12}??|00000000-0000-0000-??0000-000000000000)$/??i

回答by Souhaieb

thanks to @usertatha with some modification

感谢@usertatha 做了一些修改

function isUUID ( uuid ) {
    let s = "" + uuid;

    s = s.match('^[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}$');
    if (s === null) {
      return false;
    }
    return true;
}

回答by B. Zoli

I think Gambol's answeris almost perfect, but it misinterprets the RFC 4122 § 4.1.1. Variant sectiona bit.

我认为Gambol 的回答几乎是完美的,但它误解了RFC 4122 § 4.1.1。变体部分

It covers Variant-1 UUIDs (10xx = 8..b), but does not cover Variant-0 (0xxx = 0..7) and Variant-2 (110x = c..d) variants which are reserved for backward compatibility, so they are technically valid UUIDs. Variant-4 (111x = e..f) is indeed reserved for future use, so they are not valid currently.

它涵盖了 Variant-1 UUIDs (10xx = 8..b),但不包括 Variant-0 (0xxx = 0..7) 和 Variant-2 (110x = c..d) 变体,这些变体是为向后兼容而保留的,所以它们在技术上是有效的 UUID。Variant-4 (111x = e..f) 确实保留供将来使用,因此它们目前无效。

Also, 0 type is not valid, that "digit" is only allowed to be 0 if it's a NIL UUID (like mentioned in Evan's answer).

此外, 0 类型无效,如果“数字”是 NIL UUID(如Evan 的回答中提到),则该“数字”仅允许为 0 。

So I think the most accurate regex that complies with current RFC 4122 specification is (including hyphens):

所以我认为符合当前 RFC 4122 规范的最准确的正则表达式是(包括连字符):

/^([0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[0-9a-d][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i
                            ^                ^^^^^^
                    (0 type is not valid)  (only e..f variant digit is invalid currently)

回答by userTatha

Use the .match() method to check whether String is UUID.

使用 .match() 方法检查 String 是否为 UUID。

public boolean isUUID(String s){
    return s.match("^[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}$");
}

回答by James Morrison

A slightly modified version of the above answers written in a more concise way. This will validate any GUID with hyphens (however easily modified to make hyphens optional). This will also support upper and lower case characters which has become the convention regardless of the specification:

以更简洁的方式编写的上述答案的略微修改版本。这将验证任何带有连字符的 GUID(但很容易修改以使连字符成为可选)。这也将支持大小写字符,无论规范如何,都已成为惯例:

/^([0-9a-fA-F]{8})-(([0-9a-fA-F]{4}\-){3})([0-9a-fA-F]{12})$/i

The key here is the repeating part below

这里的关键是下面的重复部分

(([0-9a-fA-F]{4}\-){3})

Which simply repeats the 4 char patterns 3 times

简单地重复 4 个字符模式 3 次