What is the difference between `/: foo *` and `/: foo (. *)` In express routes?

In the expression, we can define some endpoints with some paths:

app.get('/:foo*', function(req, res) { ... });

app.get('/:foo(.*)', function(req, res) { ... });

The two paths are very similar, what's the difference between them?

+4
source share
1 answer

* matches zero or more of previous tokens

Tailored string /:foo/apple/banana/:foo/:1234

/:foo* matches: /:foo/apple/banana/:foo/:1234
                ^^^^^             ^^^^^

(.*)- a capture group that will match all 0 or more of the previous characters. The symbol in question is a wild card, which means that when we see /:foo, we will continue to match until we reach the end of the line

Tailored string /hello/world/:foo/bar?id=123

/:foo(.*) matches /hello/world/:foo/bar?id=123
                              ^^^^^^^^^^^^^^^^
+1
source

All Articles