正则表达式跳过所有标记并在剩余文本中搜索

时间:2014-01-29 11:53:03

标签: javascript regex mongodb pymongo

我正在使用MongoDB,我有一个名为公司的集合,它的文档如下 -

    {
        "_id":1,
        "name": "Innovative Software pvt. limited 1",
        "description": "This is a software company"
    }

    {
        "_id":2,
        "name": "Innovative Software pvt. limited 2",
        "description": "This is a software,company with <img src='' class='' alt='company logo' /> symbol"
    }

    {
        "_id":3,
        "name": "Innovative Software pvt. limited 3",
        "description": "This is a software, company with <img src='' class='' alt='company,logo' /> symbol"
    }

    {
        "_id":4,
        "name": "Innovative Software pvt. limited 4",
        "description": "This is a software, company with,<img src='' class='' alt='company, logo' /> symbol"
    }

现在我想要一个正则表达式来查找所有公司 decsription 字段满足以下条件 -

1 - 逗号与其后面的字母/数字/图像之间没有空格。
2 - 它不应包含img标签内的内容。

所以在我的情况下,我想要以下文件的输出 -
_id:2(“description”:“这是一个软件 e,c ompany with ...,

_id:4(“说明”:“这是一个软件,公司机智 h,&lt; ..

我想要一个像 -

这样的查询
db.Companies.find({description:{$regex:'regular expression'}})

可以在查询本身中实现,或者我需要在代码本身内编写逻辑。我正在使用pymongo。

1 个答案:

答案 0 :(得分:2)

试试这个正则表达式:

/"description"\s*:\s*"(?:[^<'"]|\\")+?,(?=[<a-z]).+"/gi

描述

Regular expression visualization

演示

http://regex101.com/r/bN3uY7