【问题标题】:HTML5 pattern for validating IP with mask使用掩码验证 IP 的 HTML5 模式
【发布时间】:2014-11-25 00:15:21
【问题描述】:

我正在尝试为带有 IP 地址和掩码的输入文本创建一个 HTML5 模式,如下所示:

10.120.15.30/28 或 172.181.30.0/24

我在http://html5pattern.com/Miscs 找到了一个 html5 模式,但仅适用于不带掩码的 IPv4

((^|\.)((25[0-5])|(2[0-4]\d)|(1\d\d)|([1-9]?\d))){4}$

我之前尝试过添加

    (\/).((([0-2])|(0-9))|(3[1-2])) 

但不工作。有什么想法吗?

【问题讨论】:

标签: regex html ip


【解决方案1】:
((^|\.)((25[0-5])|(2[0-4]\d)|(1\d\d)|([1-9]?\d))){4}/(?:\d|[12]\d|3[01])$

Demo.

我只是在您提供的模式中添加了/(?:\d|[12]\d|3[01]):

/          // match a slash
(?:        // then match either one of
    \d     // a single digit
|
    [12]\d // any number from 10 to 29
|
    3[01]  // 30 or 31
)

【讨论】:

  • 完美!我只使用 html5patterns,但使用正则表达式就像一个魅力。谢谢! :)
  • 不是en.wikipedia.org/wiki/Subnetwork#Subnet_and_host_counts中的前缀只有24-31,即(/(2[4-9]|3[01]))?
  • 我认为第一个解决方案是正确的,因为我可以将单个主机标记为 xx.xx.xx.xx/32
猜你喜欢
  • 1970-01-01
  • 2015-10-18
  • 1970-01-01
  • 1970-01-01
  • 2022-06-14
  • 1970-01-01
  • 2013-11-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多