用于使用掩码验证IP的HTML5模式

时间:2014-09-30 11:23:47

标签: regex html5 ip

我正在尝试使用和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])) 

但不起作用。有什么想法吗?

1 个答案:

答案 0 :(得分:3)

((^|\.)((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
)
相关问题