how to pass a variable to a regex

前端 未结 5 505
醉话见心
醉话见心 2020-12-21 09:59

I have a find statement like this

collSession.find({\"Venue.type\": /.*MT.*/}).toArray(function (err, _clsSession)
        {
            console.log(_clsSess         


        
5条回答
  •  -上瘾入骨i
    2020-12-21 10:34

    Take a look at this code: (I'm using mongoose)

    exports.getSearchPosts = (req, res, next) => {
    const keyword = req.body.keyword;
    Post.find({ postTitle: new RegExp( ".*" + keyword + ".*" ) }).then(posts => {
        res.render('post/search', {
            pageTitle: 'Search result for: ' + keyword,
            posts: posts,
            category: postCategory,
            posts: catPost,
        });
     }).catch(err => console.log(err));
    }
    

    I think you will find it helpful

提交回复
热议问题