我有以下数组:
UT.insert({
"chatUsers": [{
firstName: "John",
lastName: "Doe"
}, {
firstName: "Anna",
lastName: "Smith"
}, {
firstName: "Peter",
lastName: "Jones"
}
我在下面用正则表达式编写了代码来遍历数组:
var selector = {
chatUsers: [{
firstName: regExp
}, {
lastName: regExp
}]
};
console.log("UA" + UT.find(selector, options).fetch())
但它没有返回任何东西。建议我这样做。
这是我的代码:
if (searchText) {
console.log("searchText")
var regExp = buildRegExp(searchText);
var selector = {
chatUsers: [{
firstName: regExp
}, {
lastName: regExp
}]
};
console.log("UA" + UT.find(selector, options).fetch())
return UT.find(selector, options).fetch();
} else {
return UT.find({}, options).fetch();
}
});
这是我写下表达式的地方:
function buildRegExp(searchText) {
// this is a dumb implementation
var parts = searchText.trim().split(/[ \-\:]+/);
return new RegExp("(" + parts.join('|') + ")", "ig");
}
答案 0 :(得分:0)
var regExp = buildRegExp(searchText);
console.log("searchText :" + regExp)
var selector = {chatUsers: { $all: [
{ "$elemMatch" : { firstName: {$regex:regExp}, {$regex:regExp} } },
]
}}
console.log("serach Result ::"+UT.find(selector).fetch())
//This where I have wrote the expression:
function buildRegExp(searchText) {
return new RegExp("^"+searchText,"ig")
}