0

I want to execute a list of comparators in string format; For example: "false || true || false || true && false && true" Expect: true

I can use eval("false || true || false || true && false && true"), but due to security, I don't want to use it. Do we have another way to execute it?

Park Jang
  • 13
  • 4
  • Can you add some normal "(" ")" round brackets, so that we can understand is it true && false or (false || true || false || true) && false && true? – Aifos Si Prahs Nov 30 '22 at 11:04
  • How is this string generated? – adiga Nov 30 '22 at 11:06
  • You basically need to write a compiler/parser. At first glance it may seem like a big task, after all, writing a compiler is a famously difficult problem. But you're not going to be writing a compiler for C++ or Java. This is a simple language. First step is to tokenize your string which can simply be done with the regexp `[a-z]+|\|\||&&` (basically any lowercase words or two `|` characters or two `&` characters). Now you have your "program" as an array of symbols you can simply loop the array and process them. – slebetman Nov 30 '22 at 11:13
  • What are your [security concerns](https://stackoverflow.com/questions/197769/when-is-javascripts-eval-not-evil) when eval'ing the string? Notice, the new function approach in the linked dup is just a one form of eval. – Teemu Nov 30 '22 at 11:15

0 Answers0