我希望在我的上一个八位字节中只有0 | 64 | 128 | 192(对于带/ 26掩码的子网)。验证IP地址的第一个代码工作正常。但我无法得到最后一个八位字节工作的验证。不知道我在这里做错了什么
我正在寻找172.17.251.64应该被允许172.17.251.200不应被允许
<input name="ipAddress" title="Input data should be in IP Address Format" type="text" pattern="^(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))\.(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))\.(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))\.(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))$">
<input name="subnet" title="Input data should be in Subnet Format" type="text" pattern="^(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))\.(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))\.(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))\.(0|64|128|192))$">
答案 0 :(得分:2)
要了解你的错误,请访问regex101.com,并使用带有PCRE风格的/x
选项,并尝试将表达式分解为行以检查正则表达式的结构。见this demo:
^
(\d|[1-9]\d|1\d\d|2
([0-4]\d|5[0-5])
)
\.
(\d|[1-9]\d|1\d\d|2
([0-4]\d|5[0-5])
)
\.
(\d|[1-9]\d|1\d\d|2
([0-4]\d|5[0-5])
)
\.
(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5])) # THIS SHOULD BE REPLACED!!!
$
所以,使用
^(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))\.(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))\.(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))\.(0|64|128|192)$
^^^^^^^^^^^^^^
请参阅regex demo
请注意,您可以从正则表达式中移除^
和$
,因为默认情况下pattern
属性值已锚定(它附带^(?:
和{{1} })。