Javascript, RegEx to split the string to multidimentional array between parentheses -


i'm struggling split following input string array:

'((application = smtp , "server port" != 25) , (application = smtp , "server port" != 25)) or (application = pop3 , "server port" != 110) or (application = imap , "server port" != 143) , (application = imap or "server port" != 143)'.split(/\(([^)]+)\)/g)

yields:

["", "(application = smtp , "server port" != 25", " , ", "application = smtp , "server port" != 25", ") or ", "application = pop3 , "server port" != 110", " or ", "application = imap , "server port" != 143", " , ", "application = imap or "server port" != 143", ""]

but want result should like:

["", "(application = smtp , "server port" != 25) , (application = smtp , "server port" != 25")", or ", "application = pop3 , "server port" != 110", " or ", "application = imap , "server port" != 143", " , ", "application = imap or "server port" != 143", ""]

notice 1st index "(application = smtp , "server port" != 25) , (application = smtp , "server port" != 25")"

any suggestions achieve in regex?

edit formatted:

i'm having following input string

(    (      (app = smtp , "server port" != 25)       or       (app = pop3 , "server port" == 20)    )     ,       (app = smtp , "server port" != 35)  )  or  (app = pop3 , "server port" != 110)  ,  (    (app = imap , "server port" != 143)     or     (app = pop3 , "server port" == 20) )  , (app = imap or "server port" != 143) 

wants transform into:

[     [          [            'app = smtp , "server port" != 25',             'or',             'app = pop3 , "server port" == 20'          ],        'and',        'app = smtp , "server port" != 35'     ],     'or',     'app = pop3 , "server port" != 110',     'and',     [            [        'app = imap , "server port" != 143',         'or',        'app = pop3 , "server port" == 20'      ]    ],     'and',     'app = imap or "server port" != 143' ] 

as many suggested better way create flattend array. and, deal each inner array (split). but, still sake of argument if want regex 1 odd / awkward solution consider:

    var str = '((application = smtp , "server port" != 25) , (application = smtp , "server port" != 25)) or (application = pop3 , "server port" != 110) or (application = imap , "server port" != 143) , (application = imap or "server port" != 143)';        var final = str.replace(/\((?!\()/g,"['")        //replace ( [' if it's not preceded (                 .replace(/\(/g,"[")               //replace ( [                 .replace(/\)/g,"']")              //replace ) ']                  .replace(/\sand\s/g,"','and','")  //replace , ','and','                 .replace(/\sor\s/g,"','or','")    //replace or ','or','                 .replace(/'\[/g,"[")              //replace '[ [                 .replace(/\]'/g,"]")              //replace ]' ]                 .replace(/"/g,"\\\"")             //escape double quotes                 .replace(/'/g,"\"");              //replace ' "      console.log(json.parse("["+final+"]"))


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -