0

Can someone kindly explain, step by step, how the regular expression in the code below works and how this output is generated?

I want to fetch the data from the URL using a regular expression, but I do not understand how the regular expression works.

var uri = 'abc.com/?name=ajeet&age=22&sex=male&length=5'
var queryString = {};

uri.replace(
  new RegExp("([^?=&]+)(=([^&]*))?", "g"),
  function($0, $1, $2, $3) {
    console.log($0, $1, $2, $3);
    queryString[$1] = $3;
  }
);

console.log(queryString);

This is the current output:

name=ajeet name =ajeet ajeet
age=22 age =22 22
sex=male sex =male male
length=5 length =5 5
{ 'abc.com/': undefined,
  name: 'ajeet',
  age: '22',
  sex: 'male',
  length: '5' }
RobG
  • 124,520
  • 28
  • 153
  • 188

0 Answers0