I have a string
let str='(`id`=3) and (`filter`=\'test\' or (`filter`=\'1\' and `name`=\'2\'))' I need a string to become such an array
let arr=[['id','=', '3'], 'and', [['filter','=', 'test'], 'or', [['filter','=', '1'], 'and', ['name', '=', 'i']]]] I did it
let str = "((`id`=3) and (`filter`='te\\\'st' or (`filter`='1' and `name`='2')))"; let expression = []; let i = 0; let level = 0; let result = matches(str, /'(\\.|[^'])*'/ig, i, 't'); let text = result.text; let phrase = parseSQL(result.result, level, expression); console.log({phrase, text, expression}); console.log(getArray(expression)); function parseSQL(str, level, expression) { let j = 0; let result = matches(str, /\([^()]+\)/gi, j, `e${level}_`); let phrase = result.result; expression.push(result.text); if (result.result.match(/\(/)) { phrase = parseSQL(result.result, level + 1, expression); } return phrase; } function matches(str, regex, i, letter) { let result = str.replace(regex, function () { return `$${letter}${i++}`; }); let text = str.match(regex); return {result, text}; } I have an array in which there are keys with a level by which I can collect the required nested array, but I cannot figure out how to do it
id between 1 and 45orid IN(1, 6, 10)and evenid=case when X=7 or X=8 then 45 else 55 end. Then decide what to do with the subqueriesid IN(select x from tab ....). In general, this is called "parsing" and the answer "how to do it" will take more than one page. It is better to use literature, for example, "The Book of the Dragon" - Mike