Need explanation on Regular Expression
. I have below codes,
function getURLParameter(url, name) {
return (RegExp(name + '=' + '(.+?)(&|$)').exec(url)||[,null])[1];
}
var id = getURLParameter(url, 'id');
console.log(id); //1
// let say the example of url is
// index.php?id=1&user=xxx
I understand the exec()
function on js is to find the match.
What does the RegExp()
check to get the value of 1?
I have test it on regExr.com and it get the match of id=1&
.
Update Question
Sorry for the confusing question. What I need to know is on the return statement
.
What is the use of [1]
on
return (RegExp(name + '=' + '(.+?)(&|$)')
.exec(url)||[,null])[1];