Method for replacing url parameters with JS regular expressions, and replacing URLs with js Regular Expressions

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.