Categories
JavaScript Answers

How to use a regex for route matching in Express and Node.js?

Spread the love

Sometimes, we want to use a regex for route matching in Express and Node.js

In this article, we’ll look at how to use a regex for route matching in Express and Node.js.

How to use a regex for route matching in Express and Node.js?

To use a regex for route matching in Express and Node.js, we can use a regular JavaScript regex as route path argument.

For instance, we write

app.get(/^\/(discussion|page)\/(.+)/, (req, res, next) => {
  //...
});

to call app.get with a regex that matches discussion or page with any characters after either one.

Conclusion

To use a regex for route matching in Express and Node.js, we can use a regular JavaScript regex as route path argument.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *