If we want to create a regular expression that matches a string comprised of the substrings
Doe Ray Me Far Sew La Tea
in any order, and we don't want to tediously create a very long expression including every possible ordering, we may begin by trying
(Doe|Ray|Me|Far|Sew|La|Tea){7}
which matches a string containing the seven substrings in any order.
We then have the problem that the expression does not fail to match if one or more substrings is repeated or missing.
One way to ensure each substring appears only once is to add a negative lookahead to each alternative to ensure it doesn't appear in the string again ahead of the match.
(Doe(?!.*D)|Ray(?!.*R)|Me(?!.*M)|Far(?!.*F)|Sew(?!.*S)|La(?!.*L)|Tea(?!.*T)){7}
Note that only the first letter of the substring is required in the lookahead because the first letter of each substring is different.
In your particular case, the following seems to work fine although it hasn't been thoroughly tested.
The final trailing comma is made optional by (,|(?=\s*\}))
, which ensures that if there isn't a comma then there must be a '}'
ahead in the string after optional whitespace.
You may not want to make the semicolon optional as done here.
You may also want to split a long regular expression into multiple lines.
const input = `session({
secret: "asdfadf",
resave: false,
saveUninitialized: false,
});`;
const regex = /^session\s*\(\s*\{\s*((secret\s*:\s*(?<q>["'])\w*\k<q>(?!.*c)|resave\s*\:\s*false(?!.*e\s*:)|saveUninitialized\s*:\s*false(?!.*d\s*:))\s*(,|(?=\s*\}))\s*){3}\}\s*\)\s*;?$/;
console.log(regex.test(input));