Well, what I wanted with the capture of these numbers/expressions was to simulate the action of the exponential function with the signal ^
.
I got a way to do it, but it wasn’t just with regex
.
First, I had to capture everything before the ^
so that syntactically this fit the exponential function. For example:
In the string below, I would capture the (2 - 9)
, ignoring white spaces, regardless of their quantity:
log(10) + (2 - 9) ^ 5
And then I’d have to do the same with what there was after the ^
, what still in the above example would be the 5
.
After that I should apply these captures in the javascript function Math, pow()
that performs the exponentiation, according to what has already been said, would:
Math.pow((2 - 9), 5)
And finally, I should repeat this capture process>capture>concatenation until there is no more ^
in the midst of string. For so I could have an exponentiation within another exponentiation:
(2 ^ 3)^7
o Javascript
To capture what was before the ^
, the code was that:
function captureBefore(string) {
var result;
var n = string.indexOf("^");
var i = n - 1;
while (!(string.charAt(i) != " ")) {
--i;
}
if (!(/\d/.test(string.charAt(i)))) {
var nOpen = 1;
var nClose = 0;
while (nOpen != nClose) {
if (string.charAt(i - 1) == ")") {
++nOpen;
} else if (string.charAt(i - 1) == "(") {
++nClose;
}
i -= 1;
}
if (/[a-z]/.test(string.charAt(i - 1))) {
while ((/[a-zA-Z.]/.test(string.charAt(i - 1)))) {
i -= 1;
}
}
result = string.substring(i, n);
} else {
while (/\d/.test(string.charAt(i)) || /[-|+|.]/.test(string.charAt(i))) {
i -= 1;
}
result = string.substring(i + 1, n);
}
return result;
}
And for what was after:
function captureAfter(string) {
var result;
var n = string.indexOf("^");
var i = n + 1;
while (string.charAt(i) == " " || /[a-zA-Z.]/.test(string.charAt(i))) {
++i;
}
if (/[-|+]/.test(string.charAt(i))) {
++i;
}
if (!(/(\d)/.test(string.charAt(i)))) {
var nOpen = 1;
var nClose = 0;
while (nOpen != nClose) {
if (string.charAt(i + 1) == "(") {
++nOpen;
} else if (string.charAt(i + 1) == ")") {
++nClose;
}
i += 1;
}
result = (string.substring(n + 1, i + 1));
} else {
while (/\d/.test(string.charAt(i)) || /[-|+|.]/.test(string.charAt(i))) {
i += 1;
}
result = (string.substring(n + 1, i));
}
return result;
}
And for it to repeat itself and function to reach all signals:
if (/\^/g.test(string)) {
string = string.replace(/\s*\^\s*/g, "^")
while (/\^/g.test(string)) {
string = string.replace(captureBefore(string) + "^" + captureAfter(string), "Math.pow(" + captureBefore(string) + ", " + captureAfter(string) + ")");
}
}
With this I was able to perform the function successfully. But if anyone has an idea that has the same result with less code, I’m still open to possibilities.
Functional example
Try: [ ] This expression uses the denied list.
– gato
@Denercarvalho, in which case it will not only capture the string excluding the
^
?– Samir Braga
Yes, now I understand what you want, my expression won’t work.
– gato
You want to isolate only the mathematical operation that uses power
^
.– gato
That’s right. At this link there is an example that this site works, but when I insert in javascript, it does not return me the correct value.
– Samir Braga
and because the expression cannot recognize the operations that are between relatives, both the right of the power
^
as for the left, only one operation alone 98 22.– gato
It does not bring the correct values because groups are missing, see this example: https://regex101.com/r/uL2fQ0/5 But I still can’t get it to pick up other expressions. I’m trying to.
– KhaosDoctor