Method for replacing url parameters with JS regular expressions, and replacing URLs with js Regular Expressions
The Code is as follows:
/* Define the replacement Object key value */var setReferArgs = function () {var referArgs = new Object (); referArgs ['# userID \ #'] = userId; referArgs ['\ # userName \ #'] = userName; return referArgs;}/* replace the URL parameter */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 ;}
Chestnuts:
"Http: // 10.0.0.250: 8088/test? UesrID = # userID # "is replaced with the corresponding value" http: // 10.0.0.250: 8088/test? UesrID = 12345 ";
Replace "http: // 10.0.0.250: 8088/test/# userID #" with the corresponding value "http: // 10.0.0.250: 8088/12345 ";
Extension:
Js uses regular expressions to obtain parameter values from URLs
// Obtain the parameter value from the 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 ");
Summary
The above section describes how to replace url parameters with JS regular expressions. I hope this will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!