1

I have to deal with a customers Javascript Framework for a very specific task.

I have this html:

<div data-foo="(#a = #b)">...loading</div>

In Javascript I get the data-*-Attribute as a string:

foo = '(#a = #b)'

then an ajax call is made with the following answer:

#a=1, #b=1

next the tags are replaced with the values from ajax call (and the operator is replaced as well):

foo = '(1 == 1)'

then foo is evaluated with eval();

result = eval(foo)  // true

Is there a way to avoid eval()? I always have to evaluate string like '(0 == 1)' or '((0 == 0) && (1 == 0))'. I have no chance to influence servers resonse. I need a good and safe way to evaluate the strings to true or false.

EDIT:

possible strings are:

'(0 == 0)'
'(0 == 1)'
'(0 > 5)'
'(117 > 0)'
'((0 == 1) && (11 == 11))'
'((0 == 1) || (0 == 0))'
'(((0 < 1) || (0 == 0) ) && (33 != 11))'
...and so on!

the result always need to be true or false.

1 Answers1

2

Here are some resources that might solve your problem:

There are many more out there. Search "javascript expression parser".

Trevor Dixon
  • 23,216
  • 12
  • 72
  • 109
  • those expression parsers are to heavy for my application. Can someone suggest a lightweight expression parser for the strings I posted above? – user2433625 May 30 '13 at 17:08