I'm trying to convert some math related strings containing absolute values, using Regex in Javascript.
I would like to convert all occurences of |foo|
to abs(foo)
.
How can I detect if the character is opening or closing, given that they could also be nested?
Basically I would like to convert all occurrences of opening |
to abs(
and all closing |
to )
. Whatever is between the vertical bars is unchanged.
Some examples of possible input and desired output:
|x|+12
abs(x)+12
|x|+12+|x+2|
abs(x)+12+abs(x+2)
|x|+|x+|z||
abs(x)+abs(x+abs(z))
Any ideas?