javascript - Math.pow alternative "**" ES7 polyfill for IE11 - Stack Overflow

I'm trying to evaluate an expression which contains power, in string as **. i.e. eval("(22**3

I'm trying to evaluate an expression which contains power, in string as **. i.e. eval("(22**3)/12*6+3/2").The problem is Internet Explorer 11 does not recognizes this and throws syntax error. Which poly-fill I should use to overe this? Right now I'm using Modernizr 2.6.2.

example equation would be,

((1*2)*((3*(4*5)*(1+3)**(4*5))/((1+3)**(4*5)-1)-1)/6)/7
((1*2)*((3*(4*5)*(1+3)**(4*5))/((1+3)**(4*5)-1)-1)/6)/7*58+2*5
(4*5+4-5.5*5.21+14*36**2+69/0.258+2)/(12+65)

If it is not possible to do this, what are the possible alternatives?

I'm trying to evaluate an expression which contains power, in string as **. i.e. eval("(22**3)/12*6+3/2").The problem is Internet Explorer 11 does not recognizes this and throws syntax error. Which poly-fill I should use to overe this? Right now I'm using Modernizr 2.6.2.

example equation would be,

((1*2)*((3*(4*5)*(1+3)**(4*5))/((1+3)**(4*5)-1)-1)/6)/7
((1*2)*((3*(4*5)*(1+3)**(4*5))/((1+3)**(4*5)-1)-1)/6)/7*58+2*5
(4*5+4-5.5*5.21+14*36**2+69/0.258+2)/(12+65)

If it is not possible to do this, what are the possible alternatives?

Share Improve this question edited Jul 11, 2017 at 6:19 Prajwal asked Jul 11, 2017 at 6:09 PrajwalPrajwal 4,0005 gold badges28 silver badges52 bronze badges 2
  • It sounds like you need to write your own parser. Or find one that suits your needs. – Code-Apprentice Commented Jul 11, 2017 at 6:13
  • 4 A polyfill can't help you here because the code is parsed as a syntax error by old browsers. You must transform the code before it's read by the browser as code (preferably server-side using something like Babel) – Denys Séguret Commented Jul 11, 2017 at 6:13
Add a ment  | 

4 Answers 4

Reset to default 3

You cannot polyfill operators - only library members (prototypes, constructors, properties).

As your operation is confined to an eval call, you could attempt to write your own expression parser, but that would be a lot of work.

(As an aside, you shouldn't be using eval anyway, for very good reasons that I won't get into in this posting).

Another (hack-ish) option is to use a regular expression to identify trivial cases of x**y and convert them to Math.pow:

function detectAndFixTrivialPow( expressionString ) {

    var pattern = /(\w+)\*\*(\w+)/i;

    var fixed = expressionString.replace( pattern, 'Math.pow($1,$2)' );
    return fixed;
}

eval( detectAndFixTrivialPow( "foo**bar" ) );

You can use a regular expression to replace the occurrences of ** with Math.pow() invocations:

let expression = "(22**3)/12*6+3/2"
let processed = expression.replace(/(\w+)\*\*(\w+)/g, 'Math.pow($1,$2)');

console.log(processed);
console.log(eval(processed));

Things might get plicated if you start using nested or chained power expressions though.

I think you need to do some preprocessing of the input. Here is how i would approach this:

  1. Find "**" in string.
  2. Check what is on the left and right.
  3. Extract "full expressions" from left and right - if there is just a number - take it as is, and if there is a bracket - find the matching one and take whatever is inside as an expression.
  4. Replace the 2 expressions with Math.pow(left, right)

You can use Babel online to convert javascript for IE 11.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745124811a4612626.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信