The problem of obtaining url parameters by JS

problem description

 url: http://localhost:63343/IPcom/goodsDetail.html?booksId=1
 alter r  booksId=1,,1,
   booksId=1

the environmental background of the problems and what methods you have tried

related codes

/ / Please paste the code text below (do not replace the code with pictures)

 function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); 
    var r = window.location.search.substr(1).match(reg);  
    alert(r);
    if (r != null) return unescape(r[2]);

    return null; //
}

//URLbooksId
var id = getUrlParam("booksId");
console.log("id:"+id);

what result do you expect? What is the error message actually seen?

Aug.16,2021

match where there is a grouping, the first one is matched to, the second, and the third represents the content of the grouping in parentheses in you


the result of this is mainly the problem of the match () method. When it is not a global search after a regular expression

The return value of the

match () method is an array,

The first element in the

array is the matched data, and the subsequent element is the result of the regular expression subexpression matching.

your regular expression has three subexpressions, so there are three elements corresponding to three substrings in the returned array, and the other elements in the array do not answer.

the test code is as follows:

var reg = new RegExp("(^|&)num=([^&]*)(&|$)");
var url = "num=1";
var r = url.match(reg);  
console.log(r)

result:

for the definition of specific match () methods, please see w3C: http://www.w3school.com.cn/js.

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1eb8cd1-47446.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1eb8cd1-47446.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?