How do I parse and evaluate a mathematical expression in a string (e.g. \'1+1\'
) without invoking eval(string)
to yield its numerical value?
I went looking for JavaScript libraries for evaluating mathematical expressions, and found these two promising candidates:
JavaScript Expression Evaluator: Smaller and hopefully more light-weight. Allows algebraic expressions, substitutions and a number of functions.
mathjs: Allows complex numbers, matrices and units as well. Built to be used by both in-browser JavaScript and Node.js.