Search code examples
javascriptregexreplacepow

Javascript : find regexp with capture to replace a^b by Math.pow(a, b)


My string is the follow :

str = "(2+2)^(4*(5+6^(5^6))))";

As you can see, the power can be nested inside another power with or without parenthesis.

So I want to convert this string by using regexp to replace ^ by Math.pow(a,b) of javascript.

An idea ? Thank you very much in advance, cordially.


Solution

  • I think that using a regex to parse expressions will not turn out well for you...

    Why not use an math expression parser library like http://mathjs.org/