Javascript mongodb正则表达式参数
请参阅以下代码:Javascript mongodb正则表达式参数,javascript,regex,mongodb,Javascript,Regex,Mongodb,请参阅以下代码: {$or: [ {titleLong: { $regex: regExp } }, {catalog: { $regex: regExp } }, {catalogNbr: { $regex: regExp } } ]} 我正在尝试查找其字段与某个正则表达式匹配的文档。例如,有一个文档 {course: {titleLong: "Introduction to analysis of algorithms"},
{$or: [
{titleLong: { $regex: regExp } },
{catalog: { $regex: regExp } },
{catalogNbr: { $regex: regExp } }
]}
我正在尝试查找其字段与某个正则表达式匹配的文档。例如,有一个文档
{course: {titleLong: "Introduction to analysis of algorithms"},
{catalog: "cs3333"},
{catalogNbr: "3333"}}
当用户键入“intro algo”或“3333”或“cs3333”时,应返回文档。我尝试了/(intro | algo)/gi
,但它不起作用,因为它返回所有包含intro
或algo
的文档。而且,g
选项似乎不起作用。我还发现了以下正则表达式:
(?=.*\bintro\b)(?=.*\balgo\b).+
但这只会查找包含与
intro
完全相同的单词的文档,并且遗漏了introduction
删除lookahead断言中存在的单词边界,以便进行部分匹配
(?=.*intro)(?=.*algo).+
或
别忘了打开不区分大小写的修饰符i
还包括要匹配的模式“3333”
或“cs3333”
(?=.*intro).*algo.*|^(?:cs)?3333$
定义
$regex
时可以使用PCRE。您可以返回以特定单词开头的所有条目,并使用内联选项,如(?i)
(不区分大小写的搜索)。以下是一个例子:
{titleLong: { { $regex: '(?i).*\bintro.*' } }
或字符串中任何位置包含“intro”的entris:
{titleLong: { { $regex: '(?i).*intro.*' } }
然后删除单词边界
(?=.*intro)(?=.*algo)。+
删除第一个\b
以查找以值开头的单词的条目:(?=.*bintro)(?=.*balgo)。+
{titleLong: { { $regex: '(?i).*intro.*' } }