1
I have a function format(input)
which transforms code citation from a numerical expression, to become more readable to the user with Unicode characters.
e. g. "3 * 2 + 1 - 5 / 2pi" -> "3 × 2 + 1 - 5 ÷ 2π"
However, with potentiation, I want to make the number being high get overwritten (¹²³
), for that I used:
output = input.replace(/\s*\^\s*(\S+)/g, '<sup>$1</sup>');
REGEX: zero ou mais espaços, um ^, zero ou mais espaços, ( um ou mais não-espaços )
What works well for most cases where one number is being raised to another (e. g. x ^ 2 -> x²
). But if the number is raised to an expression (e. g. x ^ (2 - 6)
) ends up having an error (because of the spaces)...
So... how do I turn the powers after the ^
written on that occasion?
works correctly for this example:
2 ^ (2 - 6)
. but in another expression (e. g.2 ^ (2 - 6) + 5 / 2
he returns2 ^ <sup>(2 - 6)
 + 5 / 2</sup>
where mathematically correct would be2 ^ <sup>2 - 6</sup> + 5 / 2
(power is only exponent of the term in parentheses)– HenryPK Developing Apps
@Henrypkdevelopingapps See now.
– Sam