用于检查IP地址的javascript正则表达式
我有几个ip地址,如:用于检查IP地址的javascript正则表达式,javascript,regex,string,string-matching,Javascript,Regex,String,String Matching,我有几个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}/,但它似乎不能很好地工作 谢谢 而不是 {1-3} 你应该把 {1,3} 您的正则表达式已经有几个问题: 首先,它包含点。在正则表达式中,点
115.42.150.37
115.42.150.38
115.42.150.50
115.42.150.*
(我将能够搜索所有3个ip地址)
我现在能做的是:/[0-9]{1-3}\.[0-9]{1-3}\.[0-9]{1-3}\.[0-9]{1-3}/
,但它似乎不能很好地工作
谢谢 而不是
{1-3}
你应该把
{1,3}
您的正则表达式已经有几个问题: 首先,它包含点。在正则表达式中,点的意思是“匹配任何字符”,您只需要匹配一个实际的点。对于这个,你需要避开它,所以在点的前面放一个反斜杠 第二,但是你要匹配每个部分中的任意三个数字。这意味着您将匹配0和999之间的任何数字,其中显然包含许多无效的IP地址号码 这可以通过使数字匹配更加复杂来解决;这个网站上还有其他的答案解释了如何做到这一点,但坦率地说,这不值得付出努力——在我看来,你最好将字符串按点分割,然后只验证四个块是否为数字整数范围——即:
if(block>=0&&block试试这个..来源
匹配0.0.0.0到999.999.999.999
如果您知道seachdata不包含无效的IP地址,请使用
\b(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]?)\b
用于将IP号码与Accuracy进行匹配-4个号码中的每一个都存储在其自己的捕获组中,因此您可以稍后访问IP地址格式的正则表达式:
/^(\d\d?)|(1\d\d)|(0\d\d)|(2[0-4]\d)|(2[0-5])\.(\d\d?)|(1\d\d)|(0\d\d)|(2[0-4]\d)|(2[0-5])\.(\d\d?)|(1\d\d)|(0\d\d)|(2[0-4]\d)|(2[0-5])$/;
也许更好:
function checkIP(ip) {
var x = ip.split("."), x1, x2, x3, x4;
if (x.length == 4) {
x1 = parseInt(x[0], 10);
x2 = parseInt(x[1], 10);
x3 = parseInt(x[2], 10);
x4 = parseInt(x[3], 10);
if (isNaN(x1) || isNaN(x2) || isNaN(x3) || isNaN(x4)) {
return false;
}
if ((x1 >= 0 && x1 <= 255) && (x2 >= 0 && x2 <= 255) && (x3 >= 0 && x3 <= 255) && (x4 >= 0 && x4 <= 255)) {
return true;
}
}
return false;
}
功能检查ip(ip){
var x=ip分割(“.”),x1,x2,x3,x4;
如果(x.length==4){
x1=parseInt(x[0],10);
x2=parseInt(x[1],10);
x3=parseInt(x[2],10);
x4=parseInt(x[3],10);
if(isNaN(x1)| isNaN(x2)| isNaN(x3)| isNaN(x4)){
返回false;
}
如果((x1>=0&&x1=0&&x2=0&&x3=0&&x4如果您想要在现代浏览器中比ipv4的正则表达式更具可读性,您可以选择
function checkIsIPV4(entry) {
var blocks = entry.split(".");
if(blocks.length === 4) {
return blocks.every(function(block) {
return parseInt(block,10) >=0 && parseInt(block,10) <= 255;
});
}
return false;
}
功能检查ISIPv4(条目){
var blocks=条目分割(“.”);
if(blocks.length==4){
返回块。每个(函数(块){
return parseInt(block,10)>=0&&parseInt(block,10)试试这个,它是一个较短的版本:
^(?!0)(?!.*\.$)((1?\d?\d|25[0-5]|2[0-4]\d)(\.|$)){4}$
解释:
^ start of string
(?!0) Assume IP cannot start with 0
(?!.*\.$) Make sure string does not end with a dot
(
(
1?\d?\d| A single digit, two digits, or 100-199
25[0-5]| The numbers 250-255
2[0-4]\d The numbers 200-249
)
\.|$ the number must be followed by either a dot or end-of-string - to match the last number
){4} Expect exactly four of these
$ end of string
浏览器控制台的单元测试:
var rx=/^(?!0)(?!.*\.$)((1?\d?\d|25[0-5]|2[0-4]\d)(\.|$)){4}$/;
var valid=['1.2.3.4','11.11.11.11','123.123.123.123','255.250.249.0','1.12.123.255','127.0.0.1','1.0.0.0'];
var invalid=['0.1.1.1','01.1.1.1','012.1.1.1','1.2.3.4.','1.2.3\n4','1.2.3.4\n','259.0.0.1','123.','1.2.3.4.5','.1.2.3.4','1,2,3,4','1.2.333.4','1.299.3.4'];
valid.forEach(function(s){if (!rx.test(s))console.log('bad valid: '+s);});
invalid.forEach(function(s){if (rx.test(s)) console.log('bad invalid: '+s);});
可能会迟到,但有人可以尝试:
有效IP地址示例
115.42.150.37
192.168.0.1
110.234.52.124
210.110 – must have 4 octets
255 – must have 4 octets
y.y.y.y – only digits are allowed
255.0.0.y – only digits are allowed
666.10.10.20 – octet number must be between [0-255]
4444.11.11.11 – octet number must be between [0-255]
33.3333.33.3 – octet number must be between [0-255]
function ValidateIPaddress(ipaddress) {
if (/^(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]?)$/.test(ipaddress)) {
return (true)
}
alert("You have entered an invalid IP address!")
return (false)
}
无效IP地址的示例
115.42.150.37
192.168.0.1
110.234.52.124
210.110 – must have 4 octets
255 – must have 4 octets
y.y.y.y – only digits are allowed
255.0.0.y – only digits are allowed
666.10.10.20 – octet number must be between [0-255]
4444.11.11.11 – octet number must be between [0-255]
33.3333.33.3 – octet number must be between [0-255]
function ValidateIPaddress(ipaddress) {
if (/^(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]?)$/.test(ipaddress)) {
return (true)
}
alert("You have entered an invalid IP address!")
return (false)
}
验证IP地址的JavaScript代码
115.42.150.37
192.168.0.1
110.234.52.124
210.110 – must have 4 octets
255 – must have 4 octets
y.y.y.y – only digits are allowed
255.0.0.y – only digits are allowed
666.10.10.20 – octet number must be between [0-255]
4444.11.11.11 – octet number must be between [0-255]
33.3333.33.3 – octet number must be between [0-255]
function ValidateIPaddress(ipaddress) {
if (/^(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]?)$/.test(ipaddress)) {
return (true)
}
alert("You have entered an invalid IP address!")
return (false)
}
不要编写自己的正则表达式或复制粘贴!您可能无法覆盖所有边缘CESE(IPv6,但也包括八进制IP等)。请使用:
将返回一个布尔值
向下投票者:想解释一下为什么使用主动维护的库比从网站复制粘贴要好吗
/^(?!.*\.$)((?!0\d)(1?\d?\d|25[0-5]|2[0-4]\d)(\.|$)){4}$/
完全归功于。我会在他/她的回答下面发表评论,建议我进行双零更改,但我在这里还没有足够的声誉
更改:
- -(?!0),因为以零('0.248.42.223')开头的IPv4地址是有效的
- +(?!0\d)因为带前导零('63.14.209.00'和'011.012.013.014')的IPv4地址可以
如果您正在使用nodejs,请尝试:
require('net').isIP('10.0.0.1')
doc如果您编写了正确的代码,您只需要这个非常简单的正则表达式:/\d{1,3}/
function isIP(ip) {
let arrIp = ip.split(".");
if (arrIp.length !== 4) return "Invalid IP";
let re = /\d{1,3}/;
for (let oct of arrIp) {
if (oct.match(re) === null) return "Invalid IP"
if (Number(oct) < 0 || Number(oct) > 255)
return "Invalid IP";
}
return "Valid IP";
}
功能isIP(ip){
设arrIp=ip.拆分(“.”);
如果(arrIp.length!==4)返回“无效IP”;
设re=/\d{1,3}/;
用于(让oct进行arrIp){
如果(10月匹配(re)==null)返回“无效IP”
如果(数字(十月)<0 | |数字(十月)>255)
返回“无效IP”;
}
返回“有效IP”;
}
但实际上,通过根本不使用任何正则表达式,您可以获得更简单的代码:
function isIp(ip) {
var arrIp = ip.split(".");
if (arrIp.length !== 4) return "Invalid IP";
for (let oct of arrIp) {
if ( isNaN(oct) || Number(oct) < 0 || Number(oct) > 255)
return "Invalid IP";
}
return "Valid IP";
}
功能isIp(ip){
var arrIp=ip.分割(“.”);
如果(arrIp.length!==4)返回“无效IP”;
用于(让oct进行arrIp){
如果(伊斯南(十月)|号(十月)<0 |号(十月)>255)
返回“无效IP”;
}
返回“有效IP”;
}
总是在寻找变化,这似乎是一项重复的任务,那么使用forEach如何呢
function checkIP(ip) {
//assume IP is valid to start, once false is found, always false
var test = true;
//uses forEach method to test each block of IPv4 address
ip.split('.').forEach(validateIP4);
if (!test)
alert("Invalid IP4 format\n"+ip)
else
alert("IP4 format correct\n"+ip);
function validateIP4(num, index, arr) {
//returns NaN if not an Int
item = parseInt(num, 10);
//test validates Int, 0-255 range and 4 bytes of address
// && test; at end required because this function called for each block
test = !isNaN(item) && !isNaN(num) && item >=0 && item < 256 && arr.length==4 && test;
}
}
功能检查ip(ip){
//假设IP启动有效,一旦发现false,则始终为false
var检验=真;
//使用forEach方法测试IPv4地址的每个块
ip.split('.').forEach(validateIP4);
如果(!测试)
警报(“无效的IP4格式\n”+ip)
其他的
警报(“IP4格式正确\n”+ip);
函数validateIP4(num、index、arr){
//如果不是整数,则返回NaN
item=parseInt(num,10);
//测试验证Int、0-255范围和4字节地址
//&&test;在结束时是必需的,因为此函数为每个块调用
test=!isNaN(item)&&!isNaN(num)&&item>=0&&item<256&&arr.length==4&&test;
}
}
当测试类型而不是有效性时,不太严格。例如,当对列进行排序时,使用此检查查看要使用的排序
export const isIpAddress = (ipAddress) =>
/^((\d){1,3}\.){3}(\d){1,3}$/.test(ipAddress)
检查有效性时使用此测试。更严格的测试检查IP 8位数字是否在0-255范围内:
export const isValidIpAddress = (ipAddress) =>
/^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ipAddress)
提交迟交的稿件:
^(?!\.)((^|\.)([1-9]?\d|1\d\d|2(5[0-5]|[0-4]\d))){4}$
在我检查的答案中,它们要么更长,要么验证不完整。根据我的经验,更长意味着更难忽视,因此更容易出错。出于同样的原因,我喜欢避免重复类似的模式
当然,主要部分是测试数字-0到255,但也要确保它不允许初始零(除非是单个零):
三种备选方案-一种用于sub 100:[1-9]?\d
,一种用于100-199:1\d\d
,最后是200-255:
function isIPv4Address(inputString) {
let regex = new RegExp(/^(([0-9]{1,3}\.){3}[0-9]{1,3})$/);
if(regex.test(inputString)){
let arInput = inputString.split(".")
for(let i of arInput){
if(i.length > 1 && i.charAt(0) === '0')
return false;
else{
if(parseInt(i) < 0 || parseInt(i) >=256)
return false;
}
}
}
else
return false;
return true;
}
function isValidIP(ipaddress) {
if (/^(25[0-5]|2[0-4][0-9]|[1]?[1-9][1-9]?)\.(25[0-5]|2[0-4][0-9]|[1]?[1-9][1-9]?)\.(25[0-5]|2[0-4][0-9]|[1]?[1-9][1-9]?)\.(25[0-5]|2[0-4][0-9]|[1]?[1-9][1-9]?)$/.test(ipaddress)) {
return (true)
}
return (false) }