How do you write an arithmetic expression parser in JavaScript, without using eval or a constructor function?

后端 未结 4 2101
无人及你
无人及你 2020-12-09 21:47

Given a string:

 var str1 = \"25*5+5*7\";

Without using eval or the constructor function in JavaScript, how would I be able to

4条回答
  •  -上瘾入骨i
    2020-12-09 22:00

    You can use the expression parser of math.js:

    var str1= "25*5+5*7"
    document.write(str1 + ' = ' + math.eval(str1)); 
    // output: "25*5+5*7 = 160"

提交回复
热议问题