Posted By

ddavidd on 08/31/11


Tagged

String query


Versions (?)

Parse a query string into an object


 / Published in: JavaScript
 

URL: http://stevenbenner.com/2010/03/javascript-regex-trick-parse-a-query-string-into-an-object/

  1. var uri = window.location.href; //http://www.yoururl.com?id=test
  2. var queryString = {};
  3. uri.replace(
  4. new RegExp("([^?=&]+)(=([^&]*))?", "g"),
  5. function($0, $1, $2, $3) { queryString[$1] = $3; }
  6. );
  7. console.log(queryString['id']); //returns 'test' or 'undefined' if not found

Report this snippet  

You need to login to post a comment.