Regex eError when using {1}+ possessive quantifier in JavaScript regex

Since I am learning Javascript and Express.js at the same time I was experimenting around with regular expressions when making a get request

To familiaries my self with regular expressions I used this chart (also reproduced 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

My question is that how can I get a regex to match a url if it only has one /.
In other words, it would only match the default url localhost:1337/

app.get(/\/{1}/, function (req, res) {
    res.render("index"); 
});

However, my current regex above matches other pathnames(ie. localhost:1337/home/login) because now I know it uses the greedy quantifier

After reading more on regular expressions, I changed the quantifier so its possessive.
/\/{1}+/

But then express gave this error:

Syntax Error: Invalid Regular Expression: /\/{1}+/: Nothing to Repeat

So is my syntax for the regular expression wrong?


JavaScript does not support possessive quantifiers. The error you are seeing occurs because the + can only be used as a greedy one-or-more quantifier.

The chart you reference is from Oracle, and is explaining the quantifiers supported by Java, not JavaScript.

You don't need to resort to anything special to do the kind of matching you want.

If you want to match "a string ending in a /, with no other slashes in it, you can use:

/^[^/]+\/$/

Start of string, one or more non-slashes, followed by a slash, followed by the end of the string.