域名 ip 校验正则表达式

原文地址: http://blog.csdn.net/killapper/article/details/50586674

                   https://blog.csdn.net/junoohoome/article/details/74931993

1、ip

var reg =  /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/     
reg.test('192.168.105.301'); 
false
reg.test('192.168.105.30'); 
true

2、完整域名的正则表达式:

var reg =  /^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$/  
undefined
reg.test('baidu.com'); 
true

3、匹配网址:

var reg =  /^(?=^.{3,255}$)(http(s)?:\/\/)?(www\.)?[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+(:\d+)*(\/\w+\.\w+)*$/  
reg.test('http://www.baidu.com'); 
true
reg.test('http:/www.baidu.com'); 
false

4、匹配http url:

var reg =  /^(?=^.{3,255}$)(http(s)?:\/\/)?(www\.)?[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+(:\d+)*(\/\w+\.\w+)*([\?&]\w+=\w*)*$/ 
reg.test('http://www.tetet.com/index.html?q=1'); 
true
reg.test('http://www.tetet.com/index.html|q=1'); 
false 

 

你可能感兴趣的:(正则表达式)