Lets say logic expression looks like this:
(A || B) && (C | D)
I would like to write regular expression that searches for both ||
and |
characters inside the expression.
This is regex I currently use to find most of the other logic operators:
/AND|OR|and|or|XOR|&&|<=|<|>|>=|!=|==|&|OR*|!/gi
So how to update currently looking regex
to search for these two operators also?
Please note that it must be in similar form like it is now with operators divided with |
sign (thats where my troubles e from i guess).
I tried doing it like this:
/AND|OR|and|or|XOR|&&|<=|<|>|>=|!=|==|&|OR*|!|\/[|]\/|\/(|)*\//gi
But it doesn't matches my logic expression.
Lets say logic expression looks like this:
(A || B) && (C | D)
I would like to write regular expression that searches for both ||
and |
characters inside the expression.
This is regex I currently use to find most of the other logic operators:
/AND|OR|and|or|XOR|&&|<=|<|>|>=|!=|==|&|OR*|!/gi
So how to update currently looking regex
to search for these two operators also?
Please note that it must be in similar form like it is now with operators divided with |
sign (thats where my troubles e from i guess).
I tried doing it like this:
/AND|OR|and|or|XOR|&&|<=|<|>|>=|!=|==|&|OR*|!|\/[|]\/|\/(|)*\//gi
But it doesn't matches my logic expression.
Share Improve this question asked Mar 28, 2017 at 20:19 Ognj3nOgnj3n 7598 silver badges30 bronze badges 6-
Try
/AND|OR|and|or|XOR|&&|<=|<|>|>=|!=|==|&|OR*|!|\|{1,2}/gi
– Wiktor Stribiżew Commented Mar 28, 2017 at 20:21 - Doesn't match those characters :S – Ognj3n Commented Mar 28, 2017 at 20:24
- Really? Please show the code, there must be a problem somewhere else. – Wiktor Stribiżew Commented Mar 28, 2017 at 20:26
-
Just tack on
|\|\||\|
and you're done – user557597 Commented Mar 28, 2017 at 20:26 - @Wiktor Stribiżew left few whitespaces after expresion on same online tool so it could not find mathc.. works, thanks :) – Ognj3n Commented Mar 28, 2017 at 20:28
2 Answers
Reset to default 3try this:
AND|OR|and|or|XOR|&&|<=|<|>|>=|!=|==|&|OR*|!|[||]{2}|\|
Also you can test out these regexes, over here: http://regexr./
You may add an alternative branch like |\|{1,2}
to the regex of yours:
/AND|OR|and|or|XOR|&&|<=|>=|<|>|!=|==|&|OR*|!|\|{1,2}/gi
See the regex demo.
The \|{1,2}
matches 2 or 1 (since {1,2}
is a greedy version of a limiting quantifier, it will try to match as many |
as it can) |
symbols (this |
must be escaped since otherwise, it is an alternation operator in a regex).
If you want to enhance it (get rid of redundancy), use
/and|x?or|&&|[<>!=]=|[<>&!]|\|{1,2}/gi
See another regex demo. Note that the words might need to be matched as whole words only, and then you'd need /\b(?:and|x?or)\b|&&|[<>!=]=|[<>&!]|\|{1,2}/gi
.