简体   繁体   English

正则表达式使用javascript获取多个模式之间的匹配文本

[英]regex to get matching text between multiple patterns with javascript

Let's say i have the following string : 假设我有以下字符串:

KEYWORD_1 OR "KEYWORD COMPOSITE_2" NOT "KEYWORD COMPOSITE_3" NOT "KEYWORD_4" AND "KEYWORD_5" KEYWORD_6 KEYWORD_7 KEYWORD_8 -KEYWORD_9

The result (this is not JSON format, just a visual formatting to explain the output) i want to get with my regex(es) is to split the string to the following three arrays of keywords, each one is corresponding to a delimiter (AND, OR, NOT) and contains all the words that follows every occurrence of the delimiter. 我想用正则表达式获得的结果(这不是JSON格式,只是一种视觉格式来解释输出)是将字符串拆分为以下三个关键字数组,每个关键字数组对应一个定界符(AND ,OR,NOT),并包含每次出现分隔符后的所有单词。 Think of it like the google search field syntaxt :) : 认为它像谷歌搜索字段的语法:):

final_result = {
    {
        OR: [KEYWORD_COMPOSITE_2]
    },
    {
        AND: [
            KEYWORD_1, 
            KEYWORD_5, 
            KEYWORD_6, 
            KEYWORD_7, 
            KEYWORD_8
        ]
    },
    {
        NOT: [
            KEYWORD_COMPOSITE_3, 
            KEYWORD_4, 
            KEYWORD_9
        ]
    }
}

I am trying to do this in javascript with one or more regex. 我正在尝试使用一个或多个正则表达式在javascript中执行此操作。

Any idea ? 任何想法 ? any help ? 有什么帮助吗? thank you 谢谢

You cannot do that with regexes alone, some programming is still required: 您不能仅使用正则表达式来做到这一点,仍然需要进行一些编程:

function parse(input) {
    var keywords = ['AND', 'OR', 'NOT'];
    var result = {}, kw = 'AND';
    input.replace(/"(.+?)"|(\w+)/g, function($0, $1, $2) {
        if(keywords.indexOf($2) >= 0) {
            kw = $2;
        } else {
            if(!(kw in result))
                result[kw] = [];
            result[kw].push($1 || $2);
        }
    });
    return result;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM