JSFiddle - React, Tailwind, and code Playground
HTML
<body>
<table>
<thead>
<tr>
<td>Formula</td>
<td>Expected</td>
<td>Result</td>
</tr>
</thead>
<tbody class='output' />
</table>
</body>
CSS
div.output, div.columns {
display: block;
}
div.cell {
display: inline-block;
float: left;
width: 33%;
}
JavaScript
// copied from SO post, put in array
var examples = [
["4 ^ 3 ^ 2", "Math.pow(4, Math.pow(3, 2))"],
["4 ^ (3 ^ 2) ^ 2 ^ 1", "Math.pow(4, Math.pow(Math.pow(3, 2), Math.pow(2, 1)))"],
["4. ^ Math.sqrt(2) ^ 3", "Math.pow(4., Math.pow(Math.sqrt(2), 3))"],
["2 ^ 3e-2", "Math.pow(2, 3e-2)"],
["-5 ^ .2", "-Math.pow(5, .2)"],
["(-5) ^ 2", "Math.pow(-5, 2)"],
[".2 ^ -Infinity", "Math.pow(.2, -Infinity)"],
["2.34 ^ ((3 + 2) * Math.sin(3))", "Math.pow(2.34, (3 + 2) * Math.sin(3))"],
["Math.cos(2) ^ (3 + 2)", "Math.pow(Math.cos(2), 3 + 2)"]
];
$(function () {
// upon start
for (eCnt = 0; eCnt < examples.length; eCnt++) {
var row = $(".output").append($("<tr>"));
row.append($("<td>").html(examples[eCnt][0]));
row.append($("<td>").html(examples[eCnt][1]));
// here we use the resolve function
row.append($("<td>").html(resolve(examples[eCnt][0])));
}
});
//
// resolves the formula (^ into Math.pow)
//
function resolve(formula) {
var result = "";
// search for {operand} ^ {operatand}
var match = formula.match(/(.[^^]*)\^(.*)/);
// if matched, we have 3 entries.
if (match && match.length == 3) {
result += "Math.pow(" + resolve(match[1]) + "," + resolve(match[2]) + ")";
} else {
result += formula;
}
return result;
};