I've been stuck on some complex Regex writing. I have some textual request - like a SQL request - that I've to transform into a JSON object in Javascript. To do it, I was thinking about making a regex with a pattern which could be repeated n times.
Here is a request example:
INSERT INTO {{ruleName}} RULES {{conditions}} WHEN {{true|false}}
My main problem in here is the conditions
part as it can contain a basic condition (object.property {=|!=|<|<=|>|>=} value
) or multiple and nested conditions with {AND|OR}
or parenthesis (same rule as mathematics).
To catch the first condition, I wrote this regex:/(\s*(\w+.?\w*)\s*(=|!=|>|<|<=|>=)\s*'(.*?)'\s*)/gi
But I cannot manage to write a good regex for the multiple condition or nested condition case.
This is my regex at the moment: /(\s*(\w+.?\w*)\s*(=|!=|>|<|<=|>=)\s*'(.*?)'\s*)((\bAND\b|\bOR\b)(\s*(\w+.?\w*)\s*(=|!=|>|<|<=|>=)\s*'(.*?)'\s*)\s*){0,}/gi
Can anyone help me about it ?
EDIT: As asked, here are two examples of multiple or nested conditions
{condition1} AND {condition2} OR {condition3}
multiple conditions here{condition4} AND ({condition5} OR {condition6})
nested conditions there