Evaluating a string as a mathematical expression in JavaScript

前端 未结 16 2008
我在风中等你
我在风中等你 2020-11-22 03:34

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?

<
相关标签:
16条回答
  • 2020-11-22 04:14

    I've eventually gone for this solution, which works for summing positive and negative integers (and with a little modification to the regex will work for decimals too):

    function sum(string) {
      return (string.match(/^(-?\d+)(\+-?\d+)*$/)) ? string.split('+').stringSum() : NaN;
    }   
    
    Array.prototype.stringSum = function() {
        var sum = 0;
        for(var k=0, kl=this.length;k<kl;k++)
        {
            sum += +this[k];
        }
        return sum;
    }
    

    I'm not sure if it's faster than eval(), but as I have to carry out the operation lots of times I'm far more comfortable runing this script than creating loads of instances of the javascript compiler

    0 讨论(0)
  • 2020-11-22 04:17

    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.

    0 讨论(0)
  • 2020-11-22 04:18

    I created BigEval for the same purpose.
    In solving expressions, it performs exactly same as Eval() and supports operators like %, ^, &, ** (power) and ! (factorial). You are also allowed to use functions and constants (or say variables) inside the expression. The expression is solved in PEMDAS order which is common in programming languages including JavaScript.

    var Obj = new BigEval();
    var result = Obj.exec("5! + 6.6e3 * (PI + E)"); // 38795.17158152233
    var result2 = Obj.exec("sin(45 * deg)**2 + cos(pi / 4)**2"); // 1
    var result3 = Obj.exec("0 & -7 ^ -7 - 0%1 + 6%2"); //-7
    

    It can also be made to use those Big Number libraries for arithmetic in case you are dealing with numbers with arbitrary precision.

    0 讨论(0)
  • 2020-11-22 04:20

    Try nerdamer

    var result = nerdamer('12+2+PI').evaluate();
    document.getElementById('text').innerHTML = result.text();
    <script src="http://nerdamer.com/js/nerdamer.core.js"></script>
    <div id="text"></div>

    0 讨论(0)
  • 2020-11-22 04:20

    Here is an algorithmic solution similar to jMichael's that loops through the expression character by character and progressively tracks left/operator/right. The function accumulates the result after each turn it finds an operator character. This version only supports '+' and '-' operators but is written to be extended with other operators. Note: we set 'currOp' to '+' before looping because we assume the expression starts with a positive float. In fact, overall I'm making the assumption that input is similar to what would come from a calculator.

    function calculate(exp) {
      const opMap = {
        '+': (a, b) => { return parseFloat(a) + parseFloat(b) },
        '-': (a, b) => { return parseFloat(a) - parseFloat(b) },
      };
      const opList = Object.keys(opMap);
    
      let acc = 0;
      let next = '';
      let currOp = '+';
    
      for (let char of exp) {
        if (opList.includes(char)) {
          acc = opMap[currOp](acc, next);
          currOp = char;
          next = '';
        } else {
          next += char;
        } 
      }
    
      return currOp === '+' ? acc + parseFloat(next) : acc - parseFloat(next);
    }
    
    0 讨论(0)
  • 2020-11-22 04:24
    const operatorToFunction = {
        "+": (num1, num2) => +num1 + +num2,
        "-": (num1, num2) => +num1 - +num2,
        "*": (num1, num2) => +num1 * +num2,
        "/": (num1, num2) => +num1 / +num2
    }
    
    const findOperator = (str) => {
        const [operator] = str.split("").filter((ch) => ["+", "-", "*", "/"].includes(ch))
        return operator;
    }
    
    const executeOperation = (str) => {
        const operationStr = str.replace(/[ ]/g, "");
        const operator = findOperator(operationStr);
        const [num1, num2] = operationStr.split(operator)
        return operatorToFunction[operator](num1, num2);
    };
    
    const addition = executeOperation('1 + 1'); // ans is 2
    const subtraction = executeOperation('4 - 1'); // ans is 3
    const multiplication = executeOperation('2 * 5'); // ans is 10
    const division = executeOperation('16 / 4'); // ans is 4
    
    0 讨论(0)
提交回复
热议问题