Enter user password for conditional JavaScript statement

How can I parse a string from user input and rebuild it into a conditional javascript statement.

For example, if the user enters the string follwing in texbox

x==1 && y==2 || (z!=3)

How can I recreate this line to execute the following server side javascript

if (vars['x'] == 1 && vars['y'] == 2 || (vars['z'] != 3)) 

My application is built on node.js, if this information helps at all. I know about the eval () function, but before evaluating, I need to first transfer the variables from the user expression.

This seems too complicated for regular expressions. If anyone knows a simple parser that does something like this, that would be great.

+5
source share
1 answer

Esprima : http://esprima.org/

node .

+4

All Articles