用正则匹配/和前面的内容,我这个是不严格匹配的,贪心匹配。
<script>
var url="http://community.csdn.net/Expert/test/PostNew.asp?room=304"
alert( url.match(/^.*\//) )
</script>
<script>
var url="http://community.csdn.net/Expert/test/PostNew.asp?room=304"
alert( url.match(/^.*\//) )
</script>
<!--
var str="http://community.csdn.net/Expert/test/PostNew.asp?room=304"
var re=/.*(?=\/)/gi;
alert(str.match(re));
//-->
</script>试试这个看看。
<!--
var str="http://community.csdn.net/Expert/test/PostNew.asp?room=304"
var re=/^.*(?=\/)/gi;
alert(str.match(re));
//-->
</script>
the top is wrong ,try this
var str="http://community.csdn.net/Expert/test/PostNew.asp?room=304"
var re=/^(.*?)\/[^\/]*?$/i;
alert(str.replace(re,"$1"));
</script>
Browser.urlInfo = function(url)
{
if(/^(https?|ftp|file):\/\/([^:\/]+)(?::(\d*))?\/?(?:(.*)\/)?([^#?]*)\#?([^?]*)(?:\?(.*))?$/.test(url))
{
this.protocol = RegExp.$1;
this.hostName = RegExp.$2;
this.port = RegExp.$3;
this.path = RegExp.$4;
this.fileName = RegExp.$5;
this.hash = RegExp.$6;
this.param = RegExp.$7;
this.rootUrl = [this.protocol,"://",this.hostName,(!/^\s*$/.test(this.port))?":"+this.port:""].join('');
this.baseUrl = [this.rootUrl,this.path].join('/');
this._data = null;
}
else
return null;
}var aa = new Browser.urlInfo("http://community.csdn.net/Expert/test/PostNew.asp?room=304");//这里是调用
alert(aa.baseUrl + "/");
//还有个附带产品
Browser.urlInfo.prototype = {
getParam:function()
{
if(this._data!=null)return this._data;
var data = {};
while(/(?:\?|\&|^)([^=]*)=([^&]*)/g.exec(this.param))
{
var o = RegExp.$1;
var v = unescape(RegExp.$2);
if(data[o]==null)
{data[o]=v;}
else
{
if(data[o] instanceof Array)
data[o][data[o].length] = v;
else
data[o]=[data[o],v];
}
o=v=null;
}
return this._data = data;
}
}