regex - javascript split string by KEY WORDS and don't split if enclosed in parentheses -
i have following input string: key word => , / or
let query = `(application = http , "server port" "not in" 80,443,8080) , "client port" between 22,33 or ("file size" in 220,400,500 , "file size" "not in" 5,10,60) or "client ip" != 1.0.2.1 or application = skype`; //i want split above string following format [ '(application = http , "server port" "not in" 80,443,8080)', 'and', '"client port" between 22,33', 'or' '("file size" in 220,400,500 , "file size" "not in" 5,10,60)' 'or' '"client ip" != 1.0.2.1' 'or', 'application = skype' ] i have following code either split key words or if string enclosed in parentheses.
query.split(/and|or/g); //split based on key words ************ 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+"]")) any split string ignoring parentheses? want extract (+key words) including parentheses.
you use regex:
query.split(/(and|or)(?![^(]*\))/); this match and or or if there not ) following not preceded (. done via "negative ahead": (?! ).
with trimming of spaces (\s*) this:
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 result = str.split(/\s*(and|or)\s*(?![^(]*\))/); console.log(result); .as-console-wrapper { max-height: 100% !important; top: 0; } limitation , alternative
as noted in comments, above solution not suited expressions multiple nested pairs of parentheses. above code outputs split in first part of expression should not happen. javascript not support recursive regex patterns, you'd have code keeping nesting level:
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 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); .as-console-wrapper { max-height: 100% !important; top: 0; }
Comments
Post a Comment