Of course, we can also use regular expressions for direct matching. This article also provides an example of regular expressions.
Link decomposition method:
Copy codeThe Code is as follows:
<Script type = "text/javascript">
<! --
// Description: Javascript method for obtaining url parameters
Function getQueryString (name)
{
// If the link does not have a parameter or the link does not contain the parameter we want to obtain, a blank result is returned.
If (location. href. indexOf ("? ") =-1 | location. href. indexOf (name + '=') =-1)
{
Return '';
}
// Obtain the parameters in the Link
Var queryString = location. href. substring (location. href. indexOf ("? ") + 1 );
// What are the separation parameter pairs? Key = value & key2 = value2
Var parameters = queryString. split ("&");
Var pos, paraName, paraValue;
For (var I = 0; I <parameters. length; I ++)
{
// Obtain the equal sign position
Pos = parameters [I]. indexOf ('= ');
If (pos =-1) {continue ;}
// Obtain the name and value
ParaName = parameters [I]. substring (0, pos );
ParaValue = parameters [I]. substring (pos + 1 );
// If the query name is equal to the current name, the current value is returned. At the same time, the + sign in the link is restored to a space.
If (paraName = name)
{
Return unescape (paraValue. replace (// ++/g ,""));
}
}
Return '';
};
// Http: // localhost/test.html? Aa = bb & test = cc + dd & ee = ff
Alert (getQueryString ('test '));
// -->
</Script>
Regular Expression matching:
Copy codeThe Code is as follows:
<Script type = "text/javascript">
<! --
Function getQueryStringRegExp (name)
{
Var reg = new RegExp ("(^ | \\? | &) "+ Name +" = ([^ &] *) (\ s | & | $) "," I ");
If (reg. test (location. href) return unescape (RegExp. $2. replace (/\ +/g, ""); return "";
};
// Http: // localhost/test.html? Aa = bb & test = cc + dd & ee = ff
Alert (getQueryStringRegExp ('test '));
// -->
</Script>