开发者

Can I get an arithmetic expression as a string computed with the JavaScript Math object?

开发者 https://www.devze.com 2022-12-22 18:26 出处:网络
If I have an arithmetic expression as a string (\"2+3*4/5\"), is there a way to get this computed with the JavaScript Math开发者_JAVA百科 object without parsing that entire string to separate everythi

If I have an arithmetic expression as a string ("2+3*4/5"), is there a way to get this computed with the JavaScript Math开发者_JAVA百科 object without parsing that entire string to separate everything out?

Edit: For now, I'm just concerned about supporting +-*/ with order of operations. I'm open to eval if we can piece together a regular expression to address security.


You could use some regex parsing to check that there's nothing evil in the string, then just eval.

With just simple arithmetic operations, a safe regex would be:

s.match(/^[-*/+0-9]+$/)

Note this won't validate that the expression is balanced in terms of operands and operators (i.e. it would okay "+2*"), but it will stop any weird code injections.


eval() should be adequate, but I'd be wary of it. There is no other built-in solution, though. Just for kicks, I threw together a really simple parser for that sort of arithmetic expression, in JavaScript. Full source here: http://gist.github.com/332477

Basic mechanism is to split on each operator, in order of precedence low->high, and then recursively evaluate each chunk, with a parseInt at the base level, and combine with a simple array reduction on the results, using the operator from the split. Here's the core function (sum, negasum, dividend, product are just array reduce functions for each operator):

 calc = function(input) {
    if (input.indexOf("+") >= 0) {
      return sum(input.split("+"));
    } else if (input.indexOf("-") >= 0) {
      return negasum(input.split("-"));
    } else if (input.indexOf("*") >= 0) {
      return product(input.split("*"));
    } else if (input.indexOf("/") >= 0) {
      return dividend(input.split("/"));
    } else {
      return parseInt(input, 10);
    }
 };

Only supports positive integers, doesn't support parens.

Results for your example (from the console):

  calc("2+3*4/5") -> 4.4
  eval("2+3*4/5") -> 4.4
0

精彩评论

暂无评论...
验证码 换一张
取 消