Method of JavaScript Regular Obtaining Parameters in Address Bar

  • 2021-07-26 06:12:52
  • OfStack

In this paper, an example is given to describe the method of obtaining parameters in address bar by JavaScript regularity. Share it for your reference, as follows:

1. Issues:

Get the parameters in the address bar:

If the address in the address bar is:

http://10.124.36.56:8080/CMOD/index.jsp?name=you & password=123456 & type=student

Require the last parameter type in the address bar

2. Implementing JS:


function getAddressURLParam(paramName)
{
   // Structure 1 Objects with regular expressions of target parameters 
   var reg = new RegExp("(^|&)" + paramName + "=([^&]*)(&|$)");
   // Matching target parameters 
   var url = window.location.search.substr(1).match(reg);
  // Returns parameter values 
  if(url != null)
   return unescape(url[2]);
  return null;
}

Get the type parameter value:


var typeParem = getAddressURLParam(type);

Achieve results:

Obtain type parameter value: student

PS: Here are two very convenient regular expression tools for your reference:

JavaScript Regular Expression Online Test Tool:
http://tools.ofstack.com/regex/javascript

Regular expression online generation tool:
http://tools.ofstack.com/regex/create_reg

More readers interested in JavaScript can check out the topics of this site: "JavaScript Regular Expression Skills Encyclopedia", "JavaScript Replacement Operation Skills Summary", "JavaScript Search Algorithm Skills Summary", "JavaScript Data Structure and Algorithm Skills Summary", "JavaScript Traversal Algorithm and Skills Summary", "json Operation Skills Summary in JavaScript", "JavaScript Error and Debugging Skills Summary" and "JavaScript Mathematical Operation Usage Summary"

I hope this article is helpful to everyone's JavaScript programming.


Related articles: