Convert power

Asked

Viewed 131 times

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?

2 answers

0

Delete all spaces within parentheses in the string before applying the <sup>:

input = "2 ^ (2 - 6) + 5 ^ (2 + 3) - 6 ^ 10";
input = input.replace(/\s+(?=[^(\]]*\))/g, "");
output = input.replace(/\s*\^\s*(\S+)/g, '<sup>$1</sup>');

input = "2 ^ (2 - 6) + 5 ^ (2 + 3) - 6 ^ 10";
input = input.replace(/\s+(?=[^(\]]*\))/g, "");
output = input.replace(/\s*\^\s*(\S+)/g, '<sup>$1</sup>');
$("#teste").html(output);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="teste"></div>

  • works correctly for this example: 2 ^ (2 - 6). but in another expression (e. g. 2 ^ (2 - 6) + 5 / 2 he returns 2 ^ <sup>(2 - 6)&#xA; + 5 / 2</sup> where mathematically correct would be 2 ^ <sup>2 - 6</sup> + 5 / 2 (power is only exponent of the term in parentheses)

  • @Henrypkdevelopingapps See now.

0

I believe this REGEX will solve your problem:

\s*\^\s*(\(.*.\)|\S+)
  1. The \(.*.\) will make the match in brackets, and group its contents.
  2. The OR followed by \S+ will look for characters that are not blank spaces.

Inputs / Outputs

x ^ 2 -> x²              //x<sup>2</sup> -> x²
x ^ (2 - 6)              //x<sup>(2 - 6)</sup>
x ^ (2 - [6 + {x + y }]) //x<sup>(2 - [6 + {x + y }])</sup>
x ^ y                    //x<sup>y</sup>
3x ^ 7y                  //3x<sup>7y</sup>
z ^ y2                   //z<sup>y2</sup>
9 ^ √4                   //9<sup>√4</sup>
9 ^ 30                   //9<sup>30</sup>
2^1000000                //2<sup>1000000</sup>
x ^ (2 - 1) + 3 - 5      //x<sup>(2 - 1)</sup> + 3 - 5

The following link to the tests carried out: https://regex101.com/r/i7ORyt/3

  • is working only with that part of the code, but when adding new elements (e. g. x ^ (2 - 1) + 3 - 5) the answer is mathematically wrong: x<sup>(2 - 1) + 3 - 5</sup> where the right one would be x<sup>2 - 1</sup> + 3 - 5...

  • I corrected the response with a new REGEX, contemplating the test case not covered by the previous solution.

  • The regex worked perfectly for the question, as in x ^ 2 + 1 -> x<sup>2+1</sup> but if there were no spaces: x^2+1 that would fail. I managed to solve for what it seems to me using the following regex, changing the \S+ (not space) by \w|\d (letter or digit) : "\s*\^\s*(\(.*.\)|\w+|\d+)" :: (https://regex101.com/r/i7ORyt/3) except for recursive regex, but I don’t think this is possible.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.