简体   繁体   English

DN的javascript正则表达式

[英]javascript regular expression for DN

I wan a regex to alidate all types of possible DN's 我想要一个正则表达式来代表所有类型的DN

I create one but its not so good. 我创造了一个但不太好。

/([A-z0-9=]{1}[A-z0-9]{1})*[,??]/ and some others by changing it, but in vain. /([A-z0-9=]{1}[A-z0-9]{1})*[,??]/其他一些人通过改变它,但是徒劳无功。

Posible DN's can be 可能的DN可以

CN=abcd,CN=abcd,O=abcd,C=us
CN=abcd0520,CN=users,O=abcd,C=us
C=us

etc

I recently had a need for this, so I created one that perfectly follows the LDAPv3 distinguished name syntax at RFC-2253 . 我最近需要这个,所以我创建了一个完全遵循RFC-2253的LDAPv3专有名称语法。

Attribute Type 属性类型

An attributeType can be expressed 2 ways. attributeType可以用2种方式表示。 An alphanumeric string that starts with an alpha, validated using: 以alpha开头的字母数字字符串,使用以下方法验证:

[A-Za-z][\\w-]*

Or it can be an OID, validated using: 或者它可以是OID,使用以下方式验证:

\\d+(?:\\.\\d+)*

So attributeType validates using: 所以attributeType使用以下方式验证:

[A-Za-z][\\w-]*|\\d+(?:\\.\\d+)*

Attribute Value 属性值

An attributeValue can be expressed 3 ways. attributeValue可以用3种方式表示。 A hex string, which is a sequence of hex-pairs with a leading # . 十六进制字符串,是一对带有前导#的十六进制对的序列。 A hex string validates using: 十六进制字符串验证使用:

#(?:[\\dA-Fa-f]{2})+

Or an escaped string; 或逃脱的字符串; each non-special character is expressed "as-is" (validates using [^,=\\+<>#;\\\\"] ). Special characters can be expressed with a leading \\ (validates using \\\\[,=\\+<>#;\\\\"] ). 每个非特殊字符都表示为“原样”(使用[^,=\\+<>#;\\\\"] )。特殊字符可以用前导\\来表示(使用\\\\[,=\\+<>#;\\\\"]验证) \\\\[,=\\+<>#;\\\\"] )。 Finally any character can be expressed as a hex-pair with a leading \\ (validates using \\\\[\\dA-Fa-f]{2} ). 最后,任何字符都可以表示为带有前导\\的十六进制对(使用\\\\[\\dA-Fa-f]{2} )。 An escaped string validates using: 转义字符串使用以下方式验证:

(?:[^,=\\+<>#;\\\\"]|\\\\[,=\\+<>#;\\\\"]|\\\\[\\dA-Fa-f]{2})*

Or a quoted-string; 或者是带引号的字符串; the value starts and ends with " , and can contain any character un-escaped except \\ and " . 值以"开头和结尾" ,并且可以包含除\\"之外的任何未转义的字符。” Additionally, any of the methods from the escaped string above can be used. 另外,可以使用来自上述转义字符串的任何方法。 A quoted-string validates using: 带引号的字符串使用以下方式验证:

"(?:[^\\\\"]|\\\\[,=\\+<>#;\\\\"]|\\\\[\\dA-Fa-f]{2})*"

All combined, an attributeValue validates using: 所有组合,attributeValue使用以下方式验证:

#(?:[\\dA-Fa-f]{2})+|(?:[^,=\\+<>#;\\\\"]|\\\\[,=\\+<>#;\\\\"]|\\\\[\\dA-Fa-f]{2})*|"(?:[^\\\\"]|\\\\[,=\\+<>#;\\\\"]|\\\\[\\dA-Fa-f]{2})*"

Name component 名称组件

A name-component in BNF is: BNF中的名称组件是:

name-component = attributeTypeAndValue *("+" attributeTypeAndValue)
attributeTypeAndValue = attributeType "=" attributeValue

In RegEx is: 在RegEx中:

(?#attributeType)=(?#attributeValue)(?:\\+(?#attributeType)=(?#attributeValue))*

Replacing the (?#attributeType) and (?#attributeValue) placeholders with the values above gives us: 用上面的值替换(?#attributeType)(?#attributeValue)占位符给我们:

(?:[A-Za-z][\\w-]*|\\d+(?:\\.\\d+)*)=(?:#(?:[\\dA-Fa-f]{2})+|(?:[^,=\\+<>#;\\\\"]|\\\\[,=\\+<>#;\\\\"]|\\\\[\\dA-Fa-f]{2})*|"(?:[^\\\\"]|\\\\[,=\\+<>#;\\\\"]|\\\\[\\dA-Fa-f]{2})*")(?:\\+(?:[A-Za-z][\\w-]*|\\d+(?:\\.\\d+)*)=(?:#(?:[\\dA-Fa-f]{2})+|(?:[^,=\\+<>#;\\\\"]|\\\\[,=\\+<>#;\\\\"]|\\\\[\\dA-Fa-f]{2})*|"(?:[^\\\\"]|\\\\[,=\\+<>#;\\\\"]|\\\\[\\dA-Fa-f]{2})*"))*

Which validates a single name-component. 这验证了单个名称组件。

Distinguished name 专有名称

Finally, the BNF for a distinguished name is: 最后,BNF的杰出名称是:

name-component *("," name-component)

In RegEx is: 在RegEx中:

(?#name-component)(?:,(?#name-component))*

Replacing the (?#name-component) placeholder with the value above gives us: 用上面的值替换(?#name-component)占位符可以得到:

^(?:[A-Za-z][\\w-]*|\\d+(?:\\.\\d+)*)=(?:#(?:[\\dA-Fa-f]{2})+|(?:[^,=\\+<>#;\\\\"]|\\\\[,=\\+<>#;\\\\"]|\\\\[\\dA-Fa-f]{2})*|"(?:[^\\\\"]|\\\\[,=\\+<>#;\\\\"]|\\\\[\\dA-Fa-f]{2})*")(?:\\+(?:[A-Za-z][\\w-]*|\\d+(?:\\.\\d+)*)=(?:#(?:[\\dA-Fa-f]{2})+|(?:[^,=\\+<>#;\\\\"]|\\\\[,=\\+<>#;\\\\"]|\\\\[\\dA-Fa-f]{2})*|"(?:[^\\\\"]|\\\\[,=\\+<>#;\\\\"]|\\\\[\\dA-Fa-f]{2})*"))*(?:,(?:[A-Za-z][\\w-]*|\\d+(?:\\.\\d+)*)=(?:#(?:[\\dA-Fa-f]{2})+|(?:[^,=\\+<>#;\\\\"]|\\\\[,=\\+<>#;\\\\"]|\\\\[\\dA-Fa-f]{2})*|"(?:[^\\\\"]|\\\\[,=\\+<>#;\\\\"]|\\\\[\\dA-Fa-f]{2})*")(?:\\+(?:[A-Za-z][\\w-]*|\\d+(?:\\.\\d+)*)=(?:#(?:[\\dA-Fa-f]{2})+|(?:[^,=\\+<>#;\\\\"]|\\\\[,=\\+<>#;\\\\"]|\\\\[\\dA-Fa-f]{2})*|"(?:[^\\\\"]|\\\\[,=\\+<>#;\\\\"]|\\\\[\\dA-Fa-f]{2})*"))*)*$

Test it here 在这里测试一下

This is not only not possible, it will never work, and should not even be attempted. 这不仅是不可能的,它永远不会起作用,甚至不应该尝试。 LDAP data (distinguished name in this case) are not strings. LDAP数据(在本例中为可分辨名称)不是字符串。 A distinguished name has distinguishedName syntax, which is not a string, and comparisons must be made with using matching rules defined in the directory server schema. 可分辨名称具有distinguishedName语法,该语法不是字符串,并且必须使用目录服务器模式中定义的匹配规则进行比较。 For this reason, regular expressions and native-language comparison, relative value, and equality operations like perl's ~~ , eq and == and Java's == cannot be used with LDAP data - if a programmer attempts this, unexpected results can occur and the code is brittle, fragile, unpredictable, and does not have repeatable characteristics. 出于这个原因,正则表达式和本地语言比较,相对值和相等操作(如perl的~~eq==以及Java的==不能与LDAP数据一起使用 - 如果程序员尝试这样做,可能会发生意外结果,代码脆弱,易碎,不可预测,并且没有可重复的特征。 Language LDAP APIs that do not support matching rules cannot be used with LDAP where comparison, equality checks, and relative value ordering comparisons are required. 不支持匹配规则的语言LDAP API不能与LDAP 一起使用 ,其中需要进行比较,相等检查和相对值排序比较。

By way of example, the distinguished names " dc=example,dc=com " and " DC=example, DC=COM " are equivalent in every way from an LDAP perspective, but native language equality operators would return false . 举例来说,从LDAP角度来看,可分辨名称“ dc=example,dc=com ”和“ DC=example, DC=COM ”在各方面都是等效的,但是本机语言相等运算符将返回false

This worked for me: 这对我有用:

Expression: 表达:

^(?<RDN>(?<Key>(?:\\[0-9A-Fa-f]{2}|\\\[^=\,\\]|[^=\,\\]+)+)\=(?<Value>(?:\\[0-9A-Fa-f]{2}|\\\[^=\,\\]|[^=\,\\]+)+))(?:\s*\,\s*(?<RDN>(?<Key>(?:\\[0-9A-Fa-f]{2}|\\\[^=\,\\]|[^=\,\\]+)+)\=(?<Value>(?:\\[0-9A-Fa-f]{2}|\\\[^=\,\\]|[^=\,\\]+)+)))*$

Test: 测试:

CN=Test User Delete\0ADEL:c1104f63-0389-4d25-8e03-822a5c3616bc,CN=Deleted Objects,DC=test,DC=domain,DC=local

The expression is already Regex escaped so to avoid having to repeat all the backslashes in C# make sure you prefix the string with the non-escaped literal @ sign, ie 表达式已经是Regex转义的,所以为了避免在C#中重复所有的反斜杠,请确保在字符串前加上非转义的文字@符号,即

var dnExpression = @"...";

This will yield four groups, first a copy of the whole string, second a copy of the last RDN, third and fourth the key/value pairs. 这将产生四个组,首先是整个字符串的副本,第二个是最后一个RDN的副本,第三个和第四个是键/值对。 You can index into each key/value using the Captures collection of each group. 您可以使用每个组的Captures集合索引每个键/值。

You can also use this to validate a RDN by cutting the expression to the "(?...)" group surrounded by the usual "^...$" to required a whole value (start-end of string). 你也可以使用它来通过将表达式切换到通常的“^ ... $”所包围的“(?...)”组来验证RDN,以获得整个值(字符串的起始端)。

I've allowed a hex special character escape "\\", simple character escape "\\" or anything other than ",=\\" inside the key/value DN text. 我允许在键/值DN文本中使用十六进制特殊字符转义“\\”,简单字符转义“\\”或除“,= \\”以外的任何内容。 I'd guess this expression could be perfected by taking extra time to go through the MSDN AD standard and restrict the allowed characters to match exactly what is or is not allowed. 我猜这个表达式可以通过花费额外的时间来完成MSDN AD标准并限制允许的字符与正确或不允许的内容相匹配来完善。 But I believe this is a good start. 但我相信这是一个好的开始。

I created one. 我创造了一个。 Working great. 工作得很好。

^(\w+[=]{1}\w+)([,{1}]\w+[=]{1}\w+)*$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM