Javascript Regular Expressions Match Query String

Description

Javascript Regular Expressions Match Query String

var uri = 'http://your.domain/product.aspx?category=4&product_id=2140&query=lcd+tv';
var queryString = {};
uri.replace(/*  w w w . j a va2s.c o m*/
    new RegExp ("([^?=&]+)(=([^&]*))?" , "g" ),
    function($0, $1, $2, $3) { queryString[$1] = $3; }
);
console.log('ID: ' + queryString['product_id']);     // ID: 2140
console.log('Name: ' + queryString['product_name']); // Name: undefined
console.log('Category: ' + queryString['category']); // Category: 4



PreviousNext

Related