0

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 Answer 1

1

I believe you just do this:

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

No?

Sign up to request clarification or add additional context in comments.

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.