less than 1 minute read

/**

  • parseQstr
  • String qstr : URL의 QueryString, location.search 등
  • bool decode : 받은 값을 디코딩 처리할 것인가?(옵션) */ function parseQstr(qstr,decode){ if(decode==undefined){decode = false;} var pt = /(?:\?|&|^)([^&=]+)=([^&=]+)(?:&|$)/; //var arr = qstr.match(pt); //var arr = pt.exec(qstr); var Rs = {}; var arr,k,v; var qarr = qstr.split(‘&’); for(var i=0,m=qarr.length;i<m;i++){ arr = qarr[i].match(pt); if(arr == null) continue; k = arr[1]; if(decode){ v = arr[2]; }else{ v = decodeURIComponent(arr[2].replace(‘+’,’ ‘)); } if(!Rs.hasOwnProperty(k)){ Rs[k] = v; }else if(Rs[k].constructor == String){ Rs[k] = [Rs[k]]; Rs[k].push(v); }else{ Rs[k].push(v); } } return Rs }

var qstr = ‘?UnitId=24006&nick=공대여자&nick=공대여자2&UUN=632a4c164e425e695dbbf243d5ba201b’; var Rs = parseQstr(qstr);


결과는

  1. Rs: Object
  2. UUN: ”632a4c164e425e695dbbf243d5ba201b”
  3. UnitId: ”24006”
  4. nick: Array[2]
  5. 0: ”공대여자”
  6. 1: ”공대여자2”
  7. length: 2

이런 모양이 됨.

만들고 보니깐 _M 에 거의 같은게 있네…


🔗original-link

Updated: