NodeJS REGEX:高效正则表达式搜索多个关键字?

时间:2014-01-20 14:57:18

标签: regex node.js

我现在有一个搜索迭代我拥有的每个json对象和每个关键字。我想匹配此搜索独家,不包括在内,我猜我需要更强大的正则表达式。基本上,如果字符串包含所有关键字,则测试为true。 (顺序无所谓)

搜索“此文字”会包含以下结果:

“this text”,“this is a text”,“This Text”,“Text This”,“this is a long string and text”,“long a string with this in the middle and text”,“that这就是本文“

并否定类似于以下字符串的文字:

“该文字”,“这不是”,“未包含的文字”

这是我现在的剧本。

items.forEach(function(item) { //iterate over the items array
    var s = JSON.stringify(item); //convert each item in items to a string
    var matched = false;
    sarray.forEach(function(qs) { //take the toArray converted query and iterate over it
        var r = new RegExp(qs, "g"); //compose a regex object with the stringified query
        if(r.test(s)) { //if regex finds the keyword in the item string,
            matched = true; //set matched to true
        }
    });
    if(matched) {
        results.push(item); //push the item into the results array
    }

1 个答案:

答案 0 :(得分:2)

我会使用一个简单的函数而不是正则表达式,因为在这种情况下,需要复杂的正则表达式。

/**
 *
 * Look for all `items' inside `str'.
 *
 *@param str   the string to search inside
 *@param items all items that must appear in the string
 *
 *@return 
 *    TRUE  => All items were found
 *    FALSE => At least one item was not found
 */
function all_items_present(str, items) {
 var i;
 var len=items.length;
 var found=true;

    for(i=0;i<len;i++) {
        if(str.search(items[i])==-1) {
            found=false;
            break;
        }
    }

    return found;
}

// returns true
all_items_present(
   '{"title":"This text is foo", "location":"Austin, TX(bar)", "baz":false}',
   ['foo','bar','baz']
);

演示

http://jsfiddle.net/7FwUP/1/


以下是以无特定顺序查找foobarbaz的等效正则表达式:

^.*?(?:foo.*?bar.*?baz|foo.*?baz.*?bar|baz.*?foo.*?bar|baz.*?bar.*?foo|bar.*?baz.*?foo|bar.*?foo.*?baz).*?$

描述

Regular expression visualization