求 js 正则 替换 QueryString
url为
http://myserver/list.asp?Placeid=3&CategoryId=5&ChannelId=6
一个js动态的菜单 当点击 地区ID为10的链接之后导航到
http://myserver/list.asp?Placeid=10&CategoryId=5&ChannelId=6
其他QueryString 的值保留
需要拿js做 正则匹配出需要的QueryString部分 然后修改,
我写的正则为 Placeid=(\s|\S)*?(?=&)
在正则测试工具里成功 在js 里失败
可以参考以下代码
<script type= "text/javascript ">
function SetNavigationUrlIndustry(fcid,fcname,ccid,ccname)
{
var rsFcid = new RegExp( "Placeid=(\s|\S)*?(?=&) ", "gi ");
var QueryString = "Placeid=10 ";
var CurrentUrl = window.location.href;
alert(CurrentUrl.replace(rsFcid,QueryString));
}
</script>
<input type= "button " onClick= "SetNavigationUrlIndustry() " value= "按钮 " />
------解决方案-------------------- <script>
var a= 'http://myserver/list.asp?Placeid=3&CategoryId=5&ChannelId=6 '
var b= "10 "
alert(a.replace(/(Placeid=)(.*?)(&|$)/i, "$1 "+b+ "$3 "))
var a= 'http://myserver/list.asp?Placeid=3 '
var b= "10 "
alert(a.replace(/(Placeid=)(.*?)(&|$)/i, "$1 "+b+ "$3 "))
</script>