日期:2014-05-16  浏览次数:20410 次

正则匹配


http://www.google.com.hk/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0CC4QFjAA&url=http%3A%2F%2Fwww.cnblogs.com%2Fpeaceli%2Farchive%2F2009%2F09%2F08%2F1561113.html&ei=nyfHUOOHF8aiigfjyIGwBg&usg=AFQjCNEiqsvp4q6UMw5qIUiaI3yeL6oeLg&bvm=bv.1354675689,d.aGc&cad=rja


以上是一段google的搜索地址,
现在的问题是如何在javascript用正则匹配出:


http%3A%2F%2Fwww.cnblogs.com%2Fpeaceli%2Farchive%2F2009%2F09%2F08%2F1561113.html


这段
------解决方案--------------------
var url="http://www.google.com.hk/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0CC4QFjAA&url=http%3A%2F%2Fwww.cnblogs.com%2Fpeaceli%2Farchive%2F2009%2F09%2F08%2F1561113.html&ei=nyfHUOOHF8aiigfjyIGwBg&usg=AFQjCNEiqsvp4q6UMw5qIUiaI3yeL6oeLg&bvm=bv.1354675689,d.aGc&cad=rja";
var reg=/http(%\d)+.*?(?=&ei)/g;
url=url.match(reg);
alert(url);
这样试试
------解决方案--------------------
<script type="text/javascript">
var url="http://www.google.com.hk/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0CC4QFjAA&url=http%3A%2F%2Fwww.cnblogs.com%2Fpeaceli%2Farchive%2F2009%2F09%2F08%2F1561113.html&ei=nyfHUOOHF8aiigfjyIGwBg&usg=AFQjCNEiqsvp4q6UMw5qIUiaI3yeL6oeLg&bvm=bv.1354675689,d.aGc&cad=rja";
var reg=/&url=.+?&ei=/g;
var t=url.match(reg).toString();
var b= t.substring(5,t.length-4);
    alert(b);
</script>