0

I'm referring to a book and it has code I can't understand:

        http.createServer(function(req,res){        
         // normalize url by removing querystring, optional        
         // trailing slash, and making lowercase        
         var path = req.url.replace(/\/?(?:\?.*)?$/, '').toLowerCase(); 
        }

I have a problem in the following line:

var path = req.url.replace(/\/?(?:\?.*)?$/, '').toLowerCase();

What is that first argument of replace method?

Aayush Neupane
  • 634
  • 5
  • 22

1 Answers1

1

The first argument is the pattern, a regex on what to look for, and the second argument is to what to replace the instances with. In this case, \/? is the / character with zero or one instance of it, and (?:\?.*)? not to capture the piece where it has ? zero to unlimited times.

Jonathan Rosa
  • 701
  • 3
  • 19