How can I replace every global instance of "x[2]" with "x_2"
An extensible set of replacements:
\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\trans}{m}
{
\tl_set:Nn \l__brad_trans_tl { #1 }
\regex_replace_all:nnN { \[(.*?)\] } { \c{sb}\cB\{\1\cE\} } \l__brad_trans_tl
\regex_replace_all:nnN { a } { \c{alpha} } \l__brad_trans_tl
% other replacements
% ...
% deliver the new token list
\l__brad_trans_tl
}
\ExplSyntaxOff
\begin{document}
$\trans{x[1]+x[2]^2+a+a[3]}$
\end{document}
Inasmuch as I love doing crazy things in TeX, do the replacements in Mathematica! This will save you a huge amount of pain. For instance,
expr = (a[1] + a[2])/Sqrt[a[3]]
expr /. {a[1] -> \[Alpha], a[2] -> \[Beta], a[3] -> \[Gamma]} // TeXForm
gives \frac{\alpha +\beta }{\sqrt{\gamma }}
with no need to tweak the output.
I really can not recommend doing this, but as you ask...
\documentclass{article}
\begin{document}
\mathcode`\[="8000
\mathcode`\]="8000
{
\catcode`\[=\active \gdef[{_\bgroup}
\catcode`\]=\active \gdef]{\egroup}
}
hmmm
\[
a[1]+a[2]+b[c+d[3]] + x
\]
\end{document}