My challenge involves working with a customer's specific task in Javascript Framework.
Here is the HTML code snippet:
<div data-foo="(#a = #b)">...loading</div>
When I retrieve the data-*
-Attribute in Javascript, it comes as a string:
foo = '(#a = #b)'
An ajax call returns the following response:
#a=1, #b=1
The tags are then replaced with the values from the ajax call (including the operator):
foo = '(1 == 1)'
Subsequently, the foo
string is evaluated using eval();
result = eval(foo) // true
Is there a way to avoid using eval()
? My task involves evaluating strings like '(0 == 1)'
or '((0 == 0) && (1 == 0))'
, and I do not have control over the server response. I am looking for a secure method to evaluate these strings to either true
or false
.
EDIT:
The possible strings include:
'(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 expected result will always be true
or false
.