node.js - "OR" operator in regex, in express routing -
i want both "/" , "/home" path, direct user same page("/home").
how can write regex in express routing. tried this:
router.route('/|/home')
dose not work.
you may use
router.route(/^\/(home)?$/) the regex matches:
^- start of string\/- slash(home)?- optional sequence of literal charshome$- end of string.
basically, same /^(\/|\/home)$/ 2 alternatives, \/ , \/home both anchored @ start , end of string, optional group (i.e. (home)?) more optimal way match 2 alternatives.
note non-capturing group can used here (and preferred), tiny bit less readable: /^\/(?:home)?$/.
Comments
Post a Comment