JS正则表达式替换url参数的方法
具体代码如下所示:
/* 定义替换对象键值 */ var setReferArgs = function(){ var referArgs = new Object(); referArgs['#userID\#'] = userId; referArgs['\#userName\#'] = userName; return referArgs; } /* 替换URL的参数 */ var replaceUrlParams = function(url){ var actualUrl = ""; var referArgs = setReferArgs(); for(var key in referArgs){ var e = eval('/'+ key +'/g'); actualUrl = url.replace(e,referArgs[key]); url = actualUrl; } return actualUrl; }
栗子:
“http://10.0.0.250:8088/test?uesrID=#userID#” 替换成对应数值 “http://10.0.0.250:8088/test?uesrID=12345”;
“http://10.0.0.250:8088/test/#userID#” 替换成对应数值 “http://10.0.0.250:8088/12345”;
延伸:
js使用正则表达式从url中获取参数值
//从url中获取参数值 function getvl(name) { var reg = new RegExp("(^|\\?|&)"+ name +"=([^&]*)(\\s|&|$)", "i"); if (reg.test(location.href)) return unescape(RegExp.$2.replace(/\+/g, " ")); return ""; }; var code = getvl("code");
总结
相关推荐
山水沐光 2020-05-26
阿斌Elements 2020-06-11
wikiwater 2020-10-27
jczwilliam 2020-08-16
littleFatty 2020-08-16
Aveiox 2020-06-23
zrtlin 2020-11-09
xuebingnan 2020-11-05
heheeheh 2020-10-19
Crazyshark 2020-09-15
softwear 2020-08-21
ZGCdemo 2020-08-16
idning 2020-08-03
jinxiutong 2020-07-26
lanzhusiyu 2020-07-19
Skyline 2020-07-04