I want to validate an input field with regular expression in JavaScript, which should validate the following cases:
Valid:
A and B and C and D
(A or B) and C
(A or B or C) and D
(A or B or C or D) and E
A and (B or C) and D
A and (B or C) or (C and D)
A or (B and C)
(A and B) or (C and D)
Invalid:
A and B and C and
(A or B and C
(A or B or C) and D or
(A or B or C or D and E
A and or (B or C) and D
A and (B or (C and D)))
A (B and C)
(A and B) or C and D)
(A and B or C and D)
Basically I need some letter from A-Z(only upper-case) followed by "and" or "or" and unlimited brackets, but the opening brackets amount should match the amount of closing ones. Also after an opening bracket I should have to be able to insert only A-Z upper-case and after a closing bracket "and", "or" or A-Z upper-case should also be valid. Nested brackets shouldn't also be valid.
I've came up with this solution, but it's only validating A-Z upper-case, "and" and "or" words and brackets, so all invalid cases provided are matching my regex.
/^[A-Z(]?[A-Z]| |and|or|[(]|[A-Z]|[)]/gm