用javascript截取相应的域名方法两种,供大家参考
1.方法1:
[javascript] view plain copy
function domainURI(str){
var durl=/http:\/\/([^\/]+)\//i;
domain = str.match(durl);
return domain[1];
}
调用:var domain=domainURI(document.location.href);
console.log(domain);
2 方法2:
[javascript] view plain copy
var urlReg = /[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?/;
var url=urlReg.exec("http://www.baidu.com/aaaa");
console.log(url[0]);
[javascript] view plain copy
[javascript] view plain copy
返回结果:www.baidu.com
(*^__^*) 嘻嘻……,记下来 下次用的时候就可以拿过来用了!
补充:
校验Url 正则表达式
[javascript] view plain copy
function IsURL(url){
var strRegex = "^((https|http):\/\/)?"
+ "(((([0-9]|1[0-9]{2}|[1-9][0-9]|2[0-4][0-9]|25[0-5])[.]{1}){3}([0-9]|1[0-9]{2}|[1-9][0-9]|2[0-4][0-9]|25[0-5]))" // IP>形式的URL- 199.194.52.184
+ "|"
+ "([0-9a-zA-Z\u4E00-\u9FA5\uF900-\uFA2D-]+[.]{1})+[a-zA-Z-]+)" // DOMAIN(域名)形式的URL
+ "(:[0-9]{1,4})?" // 端口- :80
+ "((/?)|(/[0-9a-zA-Z_!~*'().;?:@&=+$,%#-]+)+/?){1}quot;;
var re=new RegExp(strRegex);
// console.log(re.test(url));
return re.test(url);
}