英文:
Regex filtering in mongoose with partial words?
问题
我正在尝试根据一个字符串进行过滤结果。
例如- 我要找到 'Sanam Suhag'。
当我键入 's su' 时,这将找到正确的结果。
但如果第一个单词包含超过1个字母,那么我必须使用完整的第一个单词,否则它将返回空数组。
router.get('/filter/:keywords', (req, res) => {
console.log('后端接收到的关键字...', req.params.keywords);
var regex = new RegExp(req.params.keywords, "i");
var itemFound = Item.find({ name: regex, "status.active": true }, { 'name': 1 }).sort({ "updated_at": -1 }).sort({ "created_at": -1 }).limit(20);
itemFound.exec((err, data) => {
console.log(data);
var result = [];
if (!err) {
if (data && data.length && data.length > 0) {
data.forEach(item => {
let itemObj = item;
result.push(itemObj);
});
}
res.send(result);
}
if (err) {
console.log(err);
}
});
});
需要查找 - Sanam Suhag
对于 's su' 查询结果 - [{name - Sanam Suhag}]
对于 'san suhag' 查询结果 - []
英文:
I am trying to filter result based on a string.
For example- i have to find 'Sanam Suhag'.
When i type 's su' <= this will find the proper result.
But if first word have more then 1 alphabet then i will have to use full first word otherwise it will return empty array.
router.get('/filter/:keywords', (req, res) => {
console.log('Keyword recieved in backend ...', req.params.keywords);
var regex = new RegExp(req.params.keywords, "i")
var itemFound = Item.find({ name: regex, "status.active": true }, { 'name': 1 }).sort({ "updated_at": -1 }).sort({ "created_at": -1 }).limit(20);
itemFound.exec((err, data) => {
console.log(data);
var result = [];
if (!err) {
if (data && data.length && data.length > 0) {
data.forEach(item => {
let itemObj = item
result.push(itemObj)
});
}
res.send(result)
}
if (err) {
console.log(err);
}
})
});
Need to find - Sanam Suhag
for 's su' query result - [{name - Sanam Suhag}]
For 'san suhag' query result - []
答案1
得分: 1
请提供正则表达式模式,根据搜索输入来实现逻辑。
总结实现步骤:
-
按空格拆分搜索输入 (
req.params.keywords
)。 -
如果第一个词 (
firstWord
) 多于1个字符,则应使用以下模式:
^(?:${firstWord})\s(?:${wordsAfterFirstWord})
- 否则,应使用以下模式:
^(?:${firstWord}[\w]+)\s(?:${wordsAfterFirstWord})
对于调用者:
var regex = getRegexForName(req.params.keywords);
英文:
You should implement the logic to provide the Regex pattern according to the search input.
To summarize the implementation:
-
Split the search input (
req.params.keywords
) by space. -
If the first word (
firstWord
) has more than 1 character, you should use this pattern:
^(?:${firstWord})\s(?:${wordsAfterFirstWord})
Demo Regex pattern 1 @ Regex 101
- Otherwise, you should use this pattern:
^(?:${firstWord}[\w]+)\s(?:${wordsAfterFirstWord})
Demo Regex pattern 2 @ Regex 101
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let value = "Sanam Sunag";
// Text search input: "s su"
console.log("Search pattern: 's su', Result: ", getRegexForName("s su").exec(value));
// Text search input: "sam su"
console.log("Search pattern: 'sam su', Result: ", getRegexForName("sam su").exec(value));
function getRegexForName(textSearch) {
let regexPattern = null;
if (!textSearch)
return new RegExp("", "i");
let words = textSearch.split(" ");
let firstWord = words[0];
let wordsAfterFirstWord = words.slice(1).join(' ');
if (words.length == 1)
return new RegExp(firstWord, "i"); // Or your desired regex pattern that match if the textSearch has single word only
if (firstWord.length > 1)
regexPattern = `^(?:${firstWord})\\s(?:${wordsAfterFirstWord})`;
else
regexPattern = `^(?:${firstWord}[\\w]+)\\s(?:${wordsAfterFirstWord})`;
return new RegExp(regexPattern, "i");
}
<!-- end snippet -->
For the caller:
var regex = getRegexForName(req.params.keywords);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论