我有一个像这样的发现声明
collSession.find({"Venue.type": /.*MT.*/}).toArray(function (err, _clsSession)
{
console.log(_clsSession);
});
它正在给出答案。但是我需要一些变量值而不是那个被编码的值MT。 怎么做到这一点? 感谢。
更新我试过像“/."+searchterm+"./” 它不起作用。
答案 0 :(得分:4)
您可以使用RegExp对象创建基于字符串的对象
,而不是使用内联语法来创建正则表达式var searchPhrase = "MT";
var regularExpression = new RegExp(".*" + searchPhrase + ".*");
collSession.find({"Venue.type": regularExpression}) [...]
答案 1 :(得分:3)
将/.*MT.*/
替换为new RegExp( ".*" + variable + ".*" )
答案 2 :(得分:1)
试试这个:
var pattern = 'concatenate string' + here,
regexp = new Regexp(pattern);
答案 3 :(得分:0)
最后我得到from here
它是“Venue.type”:新的RegExp(queryParams.et)
答案 4 :(得分:0)
看看下面的代码:(我在用猫鼬)
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));
}
我认为您会发现有帮助