I have a function format(input)
that transforms the code quotation of a numeric expression, to be in a more readable form for the user with unicode characters.
e.g. "3 * 2 + 1 - 5 / 2pi" -> "3 × 2 + 1 - 5 ÷ 2π"
However, with the potentiation, I want to make the number being elevated overwritten ( ¹²³
), for this 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)
) it ends up having an error (because of the spaces) ...
So ... how do I turn the powers after ^
into superscript, at that time?