-5

I could not understand the following code:

var req = "google.com/?x=value&y=another";
req = req.replace(/\/?(?:\?.*)?$/, '');
console.log(req);

The above code prints google.com, its removing the query parameters from the string.

can anyone explain me how the replace function doing this task?

madhu sudhan
  • 137
  • 10

1 Answers1

0

/? matches the character / literally

(?:\?.*)? Non-capturing group

\? matches the character ? literally

.* matches any character (except newline)

$ assert position at end of the string

andrescpacheco
  • 524
  • 1
  • 7
  • 24