Reputation: 11
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
Reputation: 490153
results
to be scoped to its execution context.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.\
, ?
or &
followed by name
variable, and then literal =
and then make a capturing group of every character besides &
or #
, 0 or more times.exec()
method on the new RegExp
object, with window.location.href
(the current URL) as its argument.results
variable.results[1]
.or
You are getting a GET param by its name :)
Upvotes: 5