英文:
Regex is not working as expected in MongoDB
问题
I am using MongoDB as a database. And for some use cases, I need to search by email address. So the user will enter some text (maybe a complete or incomplete email address), and then I need to filter the document. I am using regex for filtering.
if (search) {
regex = new RegExp(search, 'i');
}
And query looks like this
query: {
...(search && {
$or: [
{ 'user.firstName': regex },
{ 'user.lastName': regex },
{ 'user.email': regex },
],
}),
}
But it is not fetching anything. Even I tried it using MongoDB Compass but it didn't work. You can see prashant.singh+07/04@testunity.com exists in the DB.
But when I write the same as the search query it didn't work
{"project._id" : ObjectId('6438ccea78ceb75cf48fced8'), "user.email" : {$regex : 'prashant.singh+07/04@testunity.com', $options : "i"}}
It results in nothing.
英文:
I am using MongoDB as a database. And for some use cases, I need to search by email address. So the user will enter some text (maybe a complete or incomplete email address), and then I need to filter the document. I am using regex for filtering.
if (search) {
regex = new RegExp(search, 'i');
}
And query looks like this
query: {
...(search && {
$or: [
{ 'user.firstName': regex },
{ 'user.lastName': regex },
{ 'user.email': regex },
],
}),
}
But it is not fetching anything. Even I tried it using MongoDB Compass but it didn't work.
You can see prashant.singh+07/04@testunity.com exists in the DB.
But when I write the same as the search query it didn't work
{"project._id" : ObjectId('6438ccea78ceb75cf48fced8'), "user.email" : {$regex : 'prashant.singh+07/04@testunity.com', $options : "i"}}
It results in nothing.
答案1
得分: 1
The problem is that your regex pattern is invalid and contains symbols with special meanings in Regex.
将正则表达式模式无效,并包含在正则表达式中具有特殊含义的符号。
Paste your regex pattern in Regex101 and you will find that it is invalid because it contains unescaped \
character.
将您的正则表达式模式粘贴到Regex101中,您会发现它无效,因为它包含了未转义的\
字符。
While these symbols have special meanings in Regex:
这些符号在正则表达式中具有特殊含义:
.
- Match any character
.
- 匹配任何字符
+
- Match previous token with one or unlimited times
+
- 匹配前一个标记一次或多次
In order to fix your regex, you should escape these characters with a \
(backslash).
为了修复您的正则表达式,您应该使用\
(反斜杠)转义这些字符。
prashant\.singh\+07\/04@testunity\.com
prashant\.singh\+07\/04@testunity\.com
While in JS wise, make sure that the input is escaped for the Regex characters before filtering via the function below:
在 JavaScript 方面,请确保在通过以下函数进行过滤之前转义输入以用于正则表达式字符:
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\$&"); // $& means the whole matched string
}
if (search) {
regex = new RegExp(escapeRegExp(search), 'i');
}
Reference: Escaping (section) Regular expressions - JavaScript | MDN
参考:转义(章节)正则表达式 - JavaScript | MDN
英文:
The problem is that your regex pattern is invalid and contains symbols with special meanings in Regex.
Paste your regex pattern in Regex101 and you will find that it is invalid because it contains unescaped \
character.
While these symbols have special meanings in Regex:
.
- Match any character
+
- Match previous token with one or unlimited times
In order to fix your regex, you should escape these characters with a \
(backslash).
prashant\.singh\+07\/04@testunity\.com
While in JS wise, make sure that the input is escaped for the Regex characters before filtering via the function below:
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
}
if (search) {
regex = new RegExp(escapeRegExp(search), 'i');
}
Reference: Escaping (section) Regular expressions - JavaScript | MDN
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论