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

如何在这些网址中提取变量?
http://life.cuoluo.com/?a=512
http://life.cuoluo.com/l.php?c=1001008&a=10
http://life.cuoluo.com/v.php?a=10&c=1001008&tpcid=12505312

我想从上面这些地址中提取a的值,并保存在另一个变量中。请问应该怎么做?

------解决方案--------------------
<textarea id=textarea1>
http://life.cuoluo.com/?a=512
http://life.cuoluo.com/l.php?c=1001008&a=10
http://life.cuoluo.com/v.php?a=10&c=1001008&tpcid=12505312

</textarea>
<script>
var re=/(?:a\=)(\d+)/g
var arr = textarea1.value.match(re);
for(i=0;i <arr.length;i++)
{
var b=arr[0].replace(re, "$1 ");
var c=arr[1].replace(re, "$1 ");
var d=arr[2].replace(re, "$1 ");
}
alert(b);
alert(c);
alert(d);
</script>

------解决方案--------------------
var s = 'http://life.cuoluo.com/v.php?sfd=3&a=10&c=1001008&tpcid=12505312 ';

var r = s.match(/[\?|&]a=([^&]+)/);

alert(r[1]);