javascript按KEY WORDS分割字符串,如果括在括号

时间:2017-04-07 22:09:23

标签: javascript regex string

我有以下输入字符串:KEY WORD => AND / OR

let query = `(Application = http AND "Server Port" "Not In" 80,443,8080) 
              AND "Client Port" Between 22,33 
              OR ("File Size" In 220,400,500 AND "File Size" "Not In" 5,10,60)
              OR "Client IP" != 1.0.2.1 
              OR Application = skype`;

//I want split the above string into following format

[
  '(Application = http AND "Server Port" "Not In" 80,443,8080)', 
  'AND',
  '"Client Port" Between 22,33',
  'OR'
  '("File Size" In 220,400,500 AND "File Size" "Not In" 5,10,60)'
  'OR'
  '"Client IP" != 1.0.2.1'
  'OR',
  'Application = skype'
]

我有以下代码,要么拆分关键字,要么字符串完全括在括号中。

 query.split(/AND|OR/g); //Split based on key words

************
 var str = '((Application = smtp AND "Server Port" != 25) AND (Application = smtp AND "Server Port" != 25)) OR (Application = pop3 AND "Server Port" != 110) OR (Application = imap AND "Server Port" != 143) AND (Application = imap OR "Server Port" != 143)';

 var final = str.replace(/\((?!\()/g,"['")        //replace ( with [' if it's not preceded with (
           .replace(/\(/g,"[")               //replace ( with [
           .replace(/\)/g,"']")              //replace ) with '] 
           .replace(/\sAND\s/g,"','AND','")  //replace AND with ','AND','
           .replace(/\sOR\s/g,"','OR','")    //replace OR with ','OR','
           .replace(/'\[/g,"[")              //replace '[ with [
           .replace(/\]'/g,"]")              //replace ]' with ]
           .replace(/"/g,"\\\"")             //escape double quotes
           .replace(/'/g,"\"");              //replace ' with "
console.log(JSON.parse("["+final+"]"))

任何帮助拆分字符串忽略括号?基本上我想提取任何东西(+ KEY WORDS),包括括号。

1 个答案:

答案 0 :(得分:1)

您可以使用此正则表达式:

query.split(/(AND|OR)(?![^(]*\))/);

如果AND之后没有OR之前没有),则该匹配仅匹配((?! )。这是通过“消极向前看”来完成的:\s*

通过修剪空格(var str = '((Application = smtp AND "Server Port" != 25) AND (Application = smtp AND "Server Port" != 25)) OR (Application = pop3 AND "Server Port" != 110) OR (Application = imap AND "Server Port" != 143) AND (Application = imap OR "Server Port" != 143)'; var result = str.split(/\s*(AND|OR)\s*(?![^(]*\))/); console.log(result);)可以得到:

.as-console-wrapper { max-height: 100% !important; top: 0; }
var str = '((Application = smtp AND "Server Port" != 25) AND (Application = smtp AND "Server Port" != 25)) OR (Application = pop3 AND "Server Port" != 110) OR (Application = imap AND "Server Port" != 143) AND (Application = imap OR "Server Port" != 143)';

var result = str.split(/(\(|\)|\s*AND\s*|\s*OR\s*)/).reduce( (acc, s) => {
    if (s.length) {
        if (acc.level += s === ')' ? 1 : s === '(' ? -1 : 0) {
            acc.buf.push(s);
        } else {
            acc.out.push(acc.buf.join('') + s);
            acc.buf = [];
        }
    }
    return acc;
}, { level: 0, buf: [], out: [] } ).out;
 
console.log(result);

限制和替代

如评论中所述,上述解决方案不适用于具有多个嵌套括号对的表达式。上面的代码在表达式的第一部分中输出实际上不应该发生的分割。由于JavaScript不支持递归正则表达式模式,因此您必须使用保持嵌套级别对其进行编码:

.as-console-wrapper { max-height: 100% !important; top: 0; }
<div class="wrapper-div">
    <div class="icon-div">
        <div class="icon"></div>
    </div>
    <div class="text-div">Lorem ipsum dolor</div>
    <div class="icon-div">
        <div class="icon"></div>
    </div>
    <div class="number-div">24</div>
</div>
<div class="wrapper-div">
    <div class="icon-div">
        <div class="icon"></div>
    </div>
    <div class="text-div"><strong>I_am_the_long_trouble_maker_that_stretches</strong></div>
    <div class="icon-div">
        <div class="icon"></div>
    </div>
    <div class="number-div">8</div>
</div>
...