javascript正则表达式检查IP地址
我有几个IP地址,例如:
115.42.150.37
115.42.150.38
115.42.150.50
如果我想搜索,我应该写什么类型的正则表达式对于所有 3 个 IP 地址?例如,如果我执行 115.42.150.*
(我将能够搜索所有 3 个 IP 地址),
我现在可以做的是: /[0-9]{1 -3}\.[0-9]{1-3}\.[0-9]{1-3}\.[0-9]{1-3}/
但似乎不能好好工作。
谢谢。
I have several ip addresses like:
115.42.150.37
115.42.150.38
115.42.150.50
What type of regular expression should I write if I want to search for the all the 3 ip addresses? Eg, if I do 115.42.150.*
(I will be able to search for all 3 ip addresses)
What I can do now is something like: /[0-9]{1-3}\.[0-9]{1-3}\.[0-9]{1-3}\.[0-9]{1-3}/
but it can't seems to work well.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(27)
除了没有正则表达式的解决方案之外:
In addition to a solution without regex:
这应该有效:
This should work:
总是寻找变化,似乎是一项重复性的任务,所以使用 forEach 怎么样!
Always looking for variations, seemed to be a repetitive task so how about using forEach!
好吧,我尝试这个,我考虑了案例以及条目必须如何:
well I try this, I considered cases and how the entries had to be:
用于验证一致 IP 地址的正则表达式
示例:
控制台输出:
A regular expression for validate well conformed ip address
Example:
Console Output:
这就是我在 May 代码中使用 IP 地址验证的方式,并且它工作正常。
this is how i used ip address validation for may code, and it is working properly.
对于那些尝试验证 IPv4 和 IPv6 的人,以下代码片段利用了 @Spudley 的简洁答案。
谢谢 @Spudley
For those trying to validate IPv4 and IPv6, the following snippet makes use of @Spudley's concise answer..
Thank you @Spudley
测试类型而不是有效性时不太严格。例如,当对列进行排序时,使用此检查来查看要使用哪种排序。
检查有效性时使用此测试。更严格的测试检查 IP 8 位数字是否在 0-255 范围内:
A less stringent when testing the type not the validity. For example when sorting columns use this check to see which sort to use.
When checking for validity use this test. An even more stringent test checking that the IP 8-bit numbers are in the range 0-255:
这就是我所做的,它速度快且工作完美:
说明:首先,使用正则表达式检查 IP 格式是否正确。尽管如此,正则表达式不会检查任何值范围。
我的意思是,如果你可以使用 Javascript 来管理正则表达式,为什么不使用它呢?因此,不要使用疯狂的正则表达式,而应仅使用正则表达式来检查格式是否正确,然后检查八位位组中的每个值是否在正确的值范围内(0 到 255)。希望这对其他人有帮助。和平。
This is what I did and it's fast and works perfectly:
Explanation: First, with the regex check that the IP format is correct. Although, the regex won't check any value ranges.
I mean, if you can use Javascript to manage regex, why not use it?. So, instead of using a crazy regex, use Regex only for checking that the format is fine and then check that each value in the octet is in the correct value range (0 to 255). Hope this helps anybody else. Peace.
而不是
你应该放
And instead of
you should put
匹配 0.0.0.0 到 999.999.999.999
如果您知道 seachdata 不包含无效 IP 地址,
则使用 用于准确匹配 IP 号码 - 4 个号码中的每一个都存储到它自己的捕获组中,以便您稍后可以访问它们
matches 0.0.0.0 through 999.999.999.999
use if you know the seachdata does not contain invalid IP addresses
use to match IP numbers with accurracy - each of the 4 numbers is stored into it's own capturing group, so you can access them later
也许更好:
it is maybe better:
答案允许 IP 地址中存在前导零,但这是不正确的。
例如(“123.045.067.089”应返回 false)。
正确的方法就是这样做。
此函数不允许以零开头的 IP 地址。
The answers over allow leading zeros in Ip address, and that it is not correct.
For example ("123.045.067.089"should return false).
The correct way to do it like that.
This function will not allow zero to lead IP addresses.
不要编写自己的正则表达式或复制粘贴!您可能不会涵盖所有边缘情况(IPv6,还有八进制 IP 等)。使用 npm 中的
is-ip
包:将返回一个布尔值。
Don't write your own regex or copy paste! You probably won't cover all edge cases (IPv6, but also octal IPs, etc). Use the
is-ip
package from npm:Will return a Boolean.
试试这个。来源来自此处。
Try this one.. Source from here.
下面的解决方案不接受填充零
这是验证 IP 地址的最简洁方法,让我们对其进行分解:
事实:有效的 IP 地址有
4八位字节
,每个八位字节可以是0 - 255
之间的数字匹配
0 - 255
之间任何值的正则表达式细分25[0-5]
匹配250 - 255
2[0-4][0-9]
匹配200 - 249
1[0 -9][0-9]
匹配100 - 199
[1-9][0-9]?
匹配1 - 99
code>0
匹配0
注释: 使用
new RegExp
时,应使用\\. 而不是
\.
因为字符串会被转义两次。Below Solution doesn't accept Padding Zeros
Here is the cleanest way to validate an IP Address, Let's break it down:
Fact: a valid IP Address is has
4 octets
, each octets can be a number between0 - 255
Breakdown of Regex that matches any value between
0 - 255
25[0-5]
matches250 - 255
2[0-4][0-9]
matches200 - 249
1[0-9][0-9]
matches100 - 199
[1-9][0-9]?
matches1 - 99
0
matches0
Notes: When using
new RegExp
you should use\\.
instead of\.
since string will get escaped twice.如果您想要在现代浏览器中比 ipv4 的正则表达式更具可读性的东西,您可以使用
If you want something more readable than regex for ipv4 in modern browsers you can go with
一个简短的正则表达式:
^(?:(?:^|\.)(?:2(?:5[0-5]|[0-4]\d)|1?\d?\d) ){4}$
示例
A short RegEx:
^(?:(?:^|\.)(?:2(?:5[0-5]|[0-4]\d)|1?\d?\d)){4}$
Example
完全归功于 oriadam。我会在他/她的答案下面评论以建议我所做的双零更改,但我在这里还没有足够的声誉...
更改:
-(?!0) 因为 IPv4以零 ('0.248.42.223') 开头的地址是有效(但不是可用)
+(?!0\d) 因为带有前导零的 IPv4 地址(“63.14.209.00”和“011.012.013.014”)有时可以解释为八进制
Full credit to oriadam. I would have commented below his/her answer to suggest the double zero change I made, but I do not have enough reputation here yet...
change:
-(?!0) Because IPv4 addresses starting with zeros ('0.248.42.223') are valid (but not usable)
+(?!0\d) Because IPv4 addresses with leading zeros ('63.14.209.00' and '011.012.013.014') can sometimes be interpreted as octal
简单方法
Simple Method
IP地址格式的正则表达式:
Regular expression for the IP address format:
如果您编写正确的代码,您只需要这个非常简单的正则表达式: /\d{1,3}/
但实际上,您根本不使用任何正则表达式,可以获得更简单的代码:
If you wrtie the proper code you need only this very simple regular expression: /\d{1,3}/
But actually you get even simpler code by not using any regular expression at all:
添加迟到的贡献:
在我检查的答案中,它们的验证要么更长,要么不完整。根据我的经验,更长的时间意味着更难被忽视,因此更容易出错。出于同样的原因,我喜欢避免重复类似的模式。
当然,主要部分是对数字的测试 - 0 到 255,但也要确保它不允许初始零(除非它是单个零):
三种交替 - 一种用于 100 以下:
[1-9]?\d
,100-199:1\d\d
最后是 200-255:2(5[0-5]|[ 0-4]\d)
。在此之前是对行首 或点
.
的测试,并且整个表达式由附加的测试了4次>{4}
。对四字节表示的完整测试首先测试行首,然后进行负向预测以避免以
.
开头的地址:^(?!\.)
,并以行尾测试 ($
) 结束。在 regex101 上查看一些示例。
Throwing in a late contribution:
Of the answers I checked, they're either longer or incomplete in their verification. Longer, in my experience, means harder to overlook and therefore more prone to be erroneous. And I like to avoid repeating similar patters, for the same reason.
The main part is, of course, the test for a number - 0 to 255, but also making sure it doesn't allow initial zeroes (except for when it's a single one):
Three alternations - one for sub 100:
[1-9]?\d
, one for 100-199:1\d\d
and finally 200-255:2(5[0-5]|[0-4]\d)
.This is preceded by a test for start of line or a dot
.
, and this whole expression is tested for 4 times by the appended{4}
.This complete test for four byte representations is started by testing for start of line followed by a negative look ahead to avoid addresses starting with a
.
:^(?!\.)
, and ended with a test for end of line ($
).See some samples here at regex101.
可能会迟到,但有人可以尝试:
有效 IP 地址示例
无效 IP 地址示例
用于验证 IP 地址的 JavaScript 代码
May be late but, someone could try:
Example of VALID IP address
Example of INVALID IP address
JavaScript code to validate an IP address
试试这个,它是一个较短的版本:
解释:
浏览器控制台的单元测试:
Try this one, it's a shorter version:
Explained:
Unit test for a browser's console:
如果您使用的是nodejs,请尝试:
require('net').isIP('10.0.0.1')
doc net.isIP()
If you are using nodejs try:
require('net').isIP('10.0.0.1')
doc net.isIP()
您获得的正则表达式已经存在几个问题:
首先,它包含点。在正则表达式中,点表示“匹配任何字符”,您只需匹配实际的点。为此,您需要转义它,因此在点前面放一个反斜杠。
其次,但您要匹配每个部分中的任意三个数字。这意味着您将匹配 0 到 999 之间的任何数字,这显然包含许多无效的 IP 地址数字。
这可以通过使号码匹配更复杂来解决;该网站上还有其他答案解释了如何做到这一点,但坦率地说,这不值得付出努力——在我看来,最好将字符串按点分开,然后将四个块验证为数字整数范围——即:
希望有帮助。
The regex you've got already has several problems:
Firstly, it contains dots. In regex, a dot means "match any character", where you need to match just an actual dot. For this, you need to escape it, so put a back-slash in front of the dots.
Secondly, but you're matching any three digits in each section. This means you'll match any number between 0 and 999, which obviously contains a lot of invalid IP address numbers.
This can be solved by making the number matching more complex; there are other answers on this site which explain how to do that, but frankly it's not worth the effort -- in my opinion, you'd be much better off splitting the string by the dots, and then just validating the four blocks as numeric integer ranges -- ie:
Hope that helps.