Reputation: 878
I'm a little confused about the example on this page: http://expressjs.com/en/guide/routing.html
This route path will match abcd, abxcd, abRANDOMcd, ab123cd, and so on. app.get('/ab*cd', function(req, res) { res.send('ab*cd'); });
After typing this example into here: http://regexr.com/
I can't recreate the same behaviour and match abxyzcd.
Why is this and what is the difference in the regex and the way it is interpreted?
Upvotes: 0
Views: 57
Reputation: 3843
It appears that the ExpressJS syntax is different than standard RegEx syntax.
This is the RegEx version of that pattern:
If you don't use the lazy modifier ( "?
" ), you will get this result:
I recommend looking into the ExpressJS syntax further to find out how they differentiate lazy & non-lazy searching.
Upvotes: 2
Reputation: 695
what you got there, isn't Regex, but a string containing a wildcard. My regex isn't very good, but I believe the *
is interpreted the same way as .*
in regex.
As it says in the link you supplied:
Here are some examples of route paths based on strings.
While further down you see:
Examples of route paths based on regular expressions:
So there is basically two ways to create patterns in your routes :)
Upvotes: 1
Reputation: 1170
In normal regular expresions * means that the preceding character can be matched 0 or more times, so with the regex you gave, you could match abbcd and abbbbcd etc.
If you want to match abrandomcd you could use the regex ab.*cd
The . means match any character, and the star means match any number of them.
As far as I can tell the example on that expressjs page is using a different form of regular expression than regexr or another normal engine would.
This is highlighted in the text
The characters ?, +, *, and () are subsets of their regular expression counterparts. The hyphen (-) and the dot (.) are interpreted literally by string-based paths.
Found on the page.
Upvotes: 1