Need to create regular expression in Javascript to check the valid conditional string

Posted by user1796078 on Stack Overflow See other posts from Stack Overflow or by user1796078
Published on 2012-11-03T10:29:25Z Indexed on 2012/11/03 11:00 UTC
Read the original article Hit count: 168

Filed under:

I want to create the regular expression in javascript which will check the valid conditional string like

-1 OR (1 AND 2) AND 1

-1 OR (1 AND 2)

-1 OR 2

-1 OR 1 OR 1

-1 AND 1 AND 1

The string should not contain 'AND' and 'OR'. For example - 1 OR 2 AND 3 is invalid. -It should be (1 OR 2) AND 3 or 1 or (2 AND 3).

I tried the following Regex. It works for most of the conditions but it is failing to check the above condition.

/^(\s*\(\d+\s(AND|OR)\s\d+\)|\s*\d+)((\s*(AND|OR)\s*)(\(\d+\s(AND|OR)\s\d+\)|\s*\d+))*$/

Can anyone please help me to sort out the above problem.

© Stack Overflow or respective owner

Related posts about regex