【发布时间】:2015-04-07 14:27:47
【问题描述】:
参考下面的代码:
{$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。
【问题讨论】:
-
然后删除单词边界。
(?=.*intro)(?=.*algo).+ -
删除第一个
\b以查找以以下值开头的单词的条目:(?=.*\bintro)(?=.*\balgo).+
标签: javascript regex mongodb