正则表达式在MongoDB中的工作不如预期。

huangapple go评论72阅读模式
英文:

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.

正则表达式在MongoDB中的工作不如预期。

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.

正则表达式在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.

正则表达式在MongoDB中的工作不如预期。

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.

正则表达式在MongoDB中的工作不如预期。

答案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

Demo @ Regex 101

在 Regex 101 上的演示

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

Demo @ Regex 101

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

huangapple
  • 本文由 发表于 2023年4月17日 01:35:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76029346.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定