干吗一定要用正则?
<script language=JavaScript>
var a="http://community.csdn.net/Expert/topic/3866/3866822.xml?temp=.3145105";
var b="http://community.csdn.net/Expert/topic/3866/";
function getParent(u)
{var o=u.split("/");
var l=o.length;
if(o[l-1]!="")
o.length--;
else
o.length-=2;
alert(o.join("/")+"/");//测试用的,可以删掉
return o.join("/")+"/";
}
getParent(a)
getParent(b)
</script>
<script language=JavaScript>
var a="http://community.csdn.net/Expert/topic/3866/3866822.xml?temp=.3145105";
var b="http://community.csdn.net/Expert/topic/3866/";
function getParent(u)
{var o=u.split("/");
var l=o.length;
if(o[l-1]!="")
o.length--;
else
o.length-=2;
alert(o.join("/")+"/");//测试用的,可以删掉
return o.join("/")+"/";
}
getParent(a)
getParent(b)
</script>
a = a.replace(/(\/)[^/]+(\/)?$/, "$1");
alert(a);
<script language=JavaScript>
var a="http://community.csdn.net/Expert/topic/3866/3866822.xml?temp=.3145105";
var b="http://community.csdn.net/Expert/topic/3866/";
var c="http://community.csdn.net/Expert/topic/3866";
var d="http://community.csdn.net";
var e="http://community.csdn.net/";
function getParent(u)
{var o=u.split("/");
var l=o.length;
if(l<=4)
{alert(u);
return u;
}
if(o[l-1]!="" && o[l-1].indexOf(".")==-1)
o.length--;
else if(o[l-1]=="" || o[l-1].indexOf(".")!=-1)
o.length-=2;
alert(o.join("/")+"/");//测试用的,可以删掉
return o.join("/")+"/";
}
getParent(a)
getParent(b)
getParent(c)
getParent(d)
getParent(e)
</script>
<script language=JavaScript>
var a="http://community.csdn.net/Expert/topic/3866/3866822.xml?temp=.3145105";
var b="http://community.csdn.net/Expert/topic/3866/";
var c="http://community.csdn.net/Expert/topic/3866";
var d="http://community.csdn.net";
var e="http://community.csdn.net/";
function getParent(u)
{alert(u.replace(/(\/)[^/]+(\/)?$/, "$1"));//测试用的,可以删掉
}
getParent(a)
getParent(b)
getParent(c)
getParent(d)
getParent(e)
</script>