//1位数:
var a1 = /\d/
//2位数:
var a2 = /[1-9]\d/
//1开头3位数:
var a31 = /1\d{2}/
//2开头3位数:
var a32 = /(2[0-4]\d)|(25[0-5])/
//模拟每个数字的可能
var format = /^(a1)|(a2)|(a31)|(a32)/
//模拟总规则
var format = /^(format)(\.format){3}$/
//最终规则
var reg = /^((\d)|([1-9]\d)|(1\d{2})|((2[0-4]\d)|(25[0-5])))(\.((\d)|([1-9]\d)|(1\d{2})|((2[0-4]\d)|(25[0-5])))){3}$/
然后打开F2在console里校验一下:
var reg = /^((\d)|([1-9]\d)|(1\d{2})|((2[0-4]\d)|(25[0-5])))(\.((\d)|([1-9]\d)|(1\d{2})|((2[0-4]\d)|(25[0-5])))){3}$/
str = '0.0.0.0'
reg.test(str)//true
str = '255.255.255.255'
reg.test(str)//true
然后分别大于255:
str = '256.255.255.255'/'255.256.255.255'/'255.255.256.255'/'255.255.255.256'
reg.test(str)//false