subu
subu

Reputation: 11

Url parameters extraction

var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);

could u please explain what exactlly is happening in the above line of code.

Thanks in advance.

Upvotes: 1

Views: 236

Answers (1)

alex
alex

Reputation: 490153

  • You create a variable with a name of results to be scoped to its execution context.
  • You invoke the RegExp constructor thereby instantiating the object and pass a string to be used as the regex. You have to do it this way because you can't concatenate regex literals with outside data.
  • The regex says match any of \, ? or & followed by name variable, and then literal = and then make a capturing group of every character besides & or #, 0 or more times.
  • You call the exec() method on the new RegExp object, with window.location.href (the current URL) as its argument.
  • The return of this is assigned to your results variable.
  • The capturing group's contents (if successful) will be in results[1].

or

You are getting a GET param by its name :)

Upvotes: 5

Related Questions