Reputation: 1453
I'm struggling to split the following input string to array:
'((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)'.split(/\(([^)]+)\)/g)
Yields:
["", "(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", ""]
But what I want the result should look like:
["", "(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", ""]
notice the 1st index "(Application = smtp AND "Server Port" != 25) AND (Application = smtp AND "Server Port" != 25")"
Any suggestions to achieve this in RegEx?
EDIT Formatted:
I'm having following input string
(
(
(App = smtp AND "Server Port" != 25)
OR
(App = pop3 AND "Server Port" == 20)
)
AND
(App = smtp AND "Server Port" != 35)
)
OR
(App = pop3 AND "Server Port" != 110)
AND
(
(App = imap AND "Server Port" != 143)
OR
(App = pop3 AND "Server Port" == 20)
)
AND (App = imap OR "Server Port" != 143)
wants to transform into:
[
[
[
'App = smtp AND "Server Port" != 25',
'OR',
'App = pop3 AND "Server Port" == 20'
],
'AND',
'App = smtp AND "Server Port" != 35'
],
'OR',
'App = pop3 AND "Server Port" != 110',
'AND',
[
[
'App = imap AND "Server Port" != 143',
'OR',
'App = pop3 AND "Server Port" == 20'
]
],
'AND',
'App = imap OR "Server Port" != 143'
]
Upvotes: 1
Views: 83
Reputation: 6081
As many suggested the better way to do is to create a flattend array. And, deal with each inner array with (split). But, Still for the sake of argument if you want to do it by regex
this is one odd / awkward solution to consider:
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+"]"))
Upvotes: 1
Reputation: 6088
Try this and see if it helps:
\(([^O]+|[^(]+)\)
The complete code will be something like this:
console.log('((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)'.split(/\(([^O]+|[^(]+)\)/g))
Upvotes: 0