tags:

views:

19

answers:

1

I am writing a string parser that takes input like "{x} + 20" and evaluates the lexical value of x with the sum of 20 and returns. So

var x = 10;
var new = eval("{x} + 20".replace("/\{(\w+\}/g", "$1"));

and here "new" should equal 30.

How can this be done?

+1  A: 

I believe you just do this:

var x = 10
var y = eval("x + 20")

No?

justin.m.chase
PS "new" is a reserved keyword so if you're getting errors it could be related to that.
justin.m.chase
Also he is feeding replace a string instead of a `RegExp` object, and even then the regex is invalid (missing a `')'`)
MooGoo