I've come up with a regex to match terms that contain a certain phrase, which can include letters or numbers, and excludes a dash. For instance, test-123, test123 and test will all be matched by ^[test0-9._-]+$/gi.
However, in MongoDB, I'm not too sure how to dynamically search for it. I've tried,
const { search } = req.query;
const regex = new RegExp(`/^[${search}0-9._-]+$/`, "ig");
const results = await Test.find({ name: { $regex: regex } })
I've also tried
await Test.find({ name: { $regex: ".*[" + search + "0-9._-]+$.*", $options: "i" } })
As well as just,
const regex = new RegExp(`/^[${search}0-9._-]+$/`, "ig");
await Test.find({ name: regex });
Not sure how to get this working, what am I doing wrong?