# regular expression in texstudio

My objective is to replace math delimiters path $...$ by $$...$$

\documentclass[12pt,a4paper]{article}
\begin{document}
This is a sample text $\sin\alpha$ and $\cos\alpha.$
\begin{enumerate}
\item $\lim\limits_{x\to 0} \frac{\sin x}{x}$
\item $\tan\alpha=\frac{\sin\alpha}{\cos\alpha}$
\end{enumerate}
\end{document}


Expected output:

\documentclass[12pt,a4paper]{article}
\begin{document}
This is a sample text  $$\sin\alpha$$  and  $$\cos\alpha.$$
\begin{enumerate}
\item  $$\lim\limits_{x\to 0} \frac{\sin x}{x}$$
\item  $$\tan\alpha=\frac{\sin\alpha}{\cos\alpha}$$
\end{enumerate}
\end{document}


How can we do this in TeXstudio TeX editor?

%SCRIPT
regexParsed = editor.text().replace(/([^\$\\])\$([^\$]+)\$/gm, '$1\$$2\$$').replace(/\r/g, ''); editor.setText(regexParsed)  This approach also takes into account $$ display math and dollar signs in a text \, so that your project won't break in case there are some of them. • It doesn't work if the two 's are not on the same line (my solution too) This is a feature missing from TeXStudio regular expressions. Feb 28 at 11:47 • See this minimal example on pastebin ix.io/3QZW . You can have surprising results. You should use an interactive way, as I do. Feb 28 at 13:38 • If needed, one can always test their regular expressions in regexr.com. About double $$, if it's not desired, one can always substitute all $$ in sequence for one  and then the order substitution. Indeed, it is as easy as writing both editor.replace commands in sequence. I've faced some problems which breaking it into pieces made the regular expression a lot more easier. – FHZ Feb 28 at 18:29 • @gigiair thank you for the issue report. I edited the code to take into account this situation as well. Mar 1 at 10:11 Search and replace (Ctrl R) (^|[^\\])\([^\]+)\ with \1$$\2$$ • This won't work as expected if the OP has $$ display math, so the document will be corrupted. You need to add an additional check for this situation. Feb 28 at 10:22 • This shouldn't be the case because $$is TeX and its use is deprecated in LaTeX. It must first replace$$..$$with $..$ Feb 28 at 10:41 • Note that the main downside is that it doesn't work if the two$s aren't on the same line. It's possible with Emacs, but in TeXStudio I'm afraid it's not possible. Emacs regular expressions are much more powerful than TeXStudio regular expressions. Feb 28 at 11:06
• @gigiair I have already tested with TeXstudio 4.2.2 and as have mentioned, it doesn't works when the pair $,$ is not in the same line. Your answer is helpful to the community and I have learned something new form it. Feb 28 at 11:20