//trim的定义 String.prototype.trim = function() { var reExtraSpace = /^\s*(.*?)\s+$/; return this.replace(reExtraSpace, "$1") }; var str = "so me" ; alert(str.trim()); //还是“so me” ?
function getHost(url) { var host = "null"; if(typeof url == "undefined"|| null == url) { url = window.location.href; } var regex = /^\w+\:\/\/([^\/]*).*/; var match = url.match(regex); if(typeof match != "undefined" && null != match) { host = match[1]; } return host; }; alert(getHost("www.sina.com")); //null ?
错在哪了?
1. trim是去除字符串两端的空格,trim(" a b c ") -> "a b c"
2. 这个getHost是获取当前页面或者一个url的域名,但是你传入的不是一个url,应该这样子getHost("http://q.cnblogs.com/q/74075/");
把分给你了!~
@Coca-code: 谢谢~