【发布时间】:2018-05-23 07:37:08
【问题描述】:
我需要使用 JSON 下面的 allowedPattern、MinLength 和 MaxLength 属性来形成正则表达式。我正在使用分组,它工作正常。
1) "DomainNetBIOSName" : {
"Description" : "NetBIOS name of the domain (upto 15 characters) for users of earlier versions of Windows e.g. CORP",
"Type" : "String",
"MinLength" : "3",
"MaxLength" : "15",
"AllowedPattern" : "[a-zA-Z0-9]+"
},
从上面的 json 中使用 minlength、maxlength 和允许的模式,我将形成这样的正则表达式 - ([a-zA-Z0-9]+){3,15} 验证工作正常。
现在如果我有这样的 json -
2) "SourceCidrForRDP" : {
"Description" : "IP Cidr from which you are likely to RDP into the instances. You can add rules later by modifying the created security groups e.g. 54.32.98.160/32",
"Type" : "String",
"MinLength" : "9",
"MaxLength" : "18",
"AllowedPattern" : "^([0-9]+\.){3}[0-9]+\/[0-9]+$"
}
正则表达式的分组不适用于此示例-
From above json using same logic forming the regex like this - (^([0-9]+\.){3}[0-9]+\/[0-9]+$){9,18}$ which failing because of added length validation {9,18}.
我想使用 minLength、maxLength 和允许的模式形成一个组合的正则表达式。需要一种适用于所有此类情况的解决方案吗?
【问题讨论】:
标签: javascript regex