As I delve into learning Javascript and Express.js simultaneously, I decided to experiment with regular expressions while making a get request.
To familiarize myself with regular expressions, I referred to this chart (also shown below).
Greedy Reluctant Possessive Meaning
X? X?? X?+ X, once or not at all
X* X*? X*+ X, zero or more times
X+ X+? X++ X, one or more times
X{n} X{n}? X{n}+ X, exactly n times
X{n,} X{n,}? X{n,}+ X, at least n times
X{n,m} X{n,m}? X{n,m}+ X, at least n but not more than m times
Now, my query is - how can I create a regex pattern that matches a URL with only one /
?
Essentially, it should only match the default URL localhost:1337/
app.get(/\/{1}/, function (req, res) {
res.render("index");
});
However, the current regex pattern above matches other paths (such as localhost:1337/home/login
) due to the greedy quantifier involved.
Upon further research on regular expressions, I attempted to use a possessive quantifier.
/\/{1}+/
Unfortunately, Express threw an error:
Syntax Error: Invalid Regular Expression: /\/{1}+/: Nothing to Repeat
So, is there an issue with my regular expression syntax?