[英]Testing an ip address with regex
Im trying to test the following strings of ip addresses for validity, such as: 我正在尝试测试以下IP地址字符串的有效性,例如:
1.1.1.1/8
15.10.30.100/16
100.10.10.44/24
198.30.20.30/32
and I have the following regex that tests whether each item of ip: 并且我有以下正则表达式,用于测试IP的每个项目:
!/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]\/\/(/^\d+)/ig?)$/.test(item.trim())
But im not sure how about the part where there is a forward slash followed by a number such as /24
, /32
... which is \\/\\/(/^\\d+)/ig
. 但是我不确定在带有正斜杠后跟数字的部分,例如
/24
, /32
...就是\\/\\/(/^\\d+)/ig
。 Could anybody point what i did wrong here? 有人可以指出我在这里做错了什么吗?
I did not check the error in your regex but I here's a working one 我没有检查您的正则表达式中的错误,但我在这里工作
(?:(?:25[0-5]|[0-2]?[0-4]?[0-9]|[0-1]?[0-9]?[0-9])\.){3}(?:(?:25[0-5]|[0-2]?[0-4]?[0-9]|[0-1]?[0-9]?[0-9]))(?:\/(?:3[0-2]|[1-2]?[0-9])|$)$
The regex should check allocation blocks from 0 to 32 and IP4 without it. 正则表达式应检查从0到32的分配块以及没有IP4的分配块。
If you want to check specific allocation blocks you should use this one 如果要检查特定的分配块,则应使用此分配块
(?:(?:25[0-5]|[0-2]?[0-4]?[0-9]|[0-1]?[0-9]?[0-9])\.){3}(?:(?:25[0-5]|[0-2]?[0-4]?[0-9]|[0-1]?[0-9]?[0-9]))(?:\/(?:8|16|24|32)|$)
and filtering blocks inside the last nested non-capturing group (?:8|16|24|32)
和最后一个嵌套的非捕获组内的过滤块
(?:8|16|24|32)
You must have had trouble pasting the regex from an online regex tester to the JS code. 您可能在将正则表达式从在线正则表达式测试器粘贴到JS代码时遇到了麻烦。 However, there are issues here: 1) you have
\\/\\/
that requires //
to appear in the string, 2) you added ^
anchor close to the end of the pattern (and since it requires the start of string position, it prevented your regex from matching). 但是,这里存在一些问题:1)您有
\\/\\/
要求//
出现在字符串中,2)在模式的结尾附近添加了^
锚(并且由于它需要字符串位置的开始,所以它阻止您的正则表达式匹配)。 Besides, no need for g
and i
modifiers, you only test the whole string against the pattern that has no letters in it. 此外,不需要
g
和i
修饰符,只需针对其中没有字母的模式测试整个字符串。
Use 采用
/^(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}\/(?:[12]?\d|3[0-2])$/
See the regex demo . 参见regex演示 。
In JS, you may build the pattern dynamically for better readability: 在JS中,您可以动态构建模式以提高可读性:
var octet = "(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"; var rx = new RegExp("^" + octet + "(?:\\\\." + octet + "){3}/(?:[12]?\\\\d|3[0-2])$"); var strs = [ "1.1.1.1/8", "15.10.30.100/16", "100.10.10.44/24", "198.30.20.30/32", "1.1.1.1/0", "1.1.1.1/32", "1.1.1.1/33"]; for (var s of strs) { console.log(s, "=>", rx.test(s)); }
Pattern details 图案细节
^
- start of string ^
-字符串的开头 (?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)
- an octet pattern, 0
to 255
(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)
-八位位组模式, 0
到255
(?:\\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}
- 3 occurrences of: (?:\\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}
-3次的:
\\.
- a dot (?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)
- an octet pattern (?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)
-八位位组模式 \\/
- a slash \\/
-斜线 (?:[12]?\\d|3[0-2])
- an opptional 1
or 2
followed with any digit ( 0
to 29
, matched with [12]?\\d
), or 3
followed with digits from 0
to 2
( 30
to 32
) (?:[12]?\\d|3[0-2])
-可选1
或2
后跟任意数字( 0
到29
,与[12]?\\d
匹配),或3
后跟从0
到数字2
( 30
至32
) $
- end of string. $
-字符串结尾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.