How to get the value of product_id from the string?

Hello.
Help with js on the regular season.
There is a line "product_id=40&quantity=1". Need to get only the value which comes after product_id=.
Thanks in advance.
July 4th 19 at 23:24
2 answers
July 4th 19 at 23:26
Solution
let str = "product_id=40&quantity=1";
let id = (function(s){
 let arr = s.split("&");
 for(let i = 0; i < arr.length; i++){
 let id = arr[i].split("=");
 if(id[0] === "product_id"){
 return id[1];
}
}
})(str);
alert(id);
July 4th 19 at 23:28
function get(name){
 if(name=(new RegExp('[?&]'+encodeURIComponent(name)+'=([^&]*)')).exec(location.search))
 return decodeURIComponent(name[1]);
}

Find more questions by tags JavaScript