Please try the following code:
$string = preg_replace('/\\\\(\w+)\\\\left\\((.+?)\\\ight\\)\\^\\{([^}]+)\\}/','\\\\$1^{$3}\left($2ight)',$string);
For me, the output is:
print "$string
";
\sin^{2}\left(xight)
Another test:
$string = "\\cos\\left(3x\ight)^{2.6}";
Output:
\cos^{2.6}\left(3xight)
It also works for multiple functions, since preg_replace
is global per default.
$string = "\\sin\\left(x\ight)^{3}\\cos\\left(y\ight)^{2}";
\sin^{3}\left(xight)\cos^{2}\left(yight)
Edit: Please note the corrected expression.