# How can I highlight a freestanding regexp in string style?

I'm using listings to format a piece of JavaScript code, and I met a problem for the following code:

...
regex = /abc/;
...
ratio = 1.0/2.0;
...


I want the regex is highlighted as a string, but if I use the following definition, the division sign will be recognized as a beginning of a string:

\lstdefinelanguage{JavaScript}{
morekeywords={break,case,catch,continue,debugger,default,delete,do,else,finally,for,function,if,in,instanceof,new,return,switch,this,throw,try,typeof,var,void,while,with},
morekeywords=[2]{class,enum,export,extends,import,super,implements,interface,let,package,private,protected,public,static,yield},
morekeywords=[3]{prototype},
morestring=[b]{"},
morestring=[b]{'},
morestring=[b]{/},
morecomment=[l]{//},
morecomment=[s]{/*}{*/}
}


How can I fix this? Can I escape the division sign in some way?

Source here:

\documentclass{article}

\usepackage{tikz}
\usepackage{listings}

\begin{document}

\lstset{
basicstyle=\footnotesize\ttfamily\bfseries\color{cyan!50!black},
keywordstyle=\color{green!50!black},
keywordstyle=[2]\color{red!50!black},
stringstyle=\color{yellow!80!black},
identifierstyle=\color{blue!80!white},
escapeinside={(*@}{@*)}}

\lstdefinelanguage{JavaScript}{
morekeywords={break,case,catch,continue,debugger,default,delete,do,else,finally,for,function,if,in,instanceof,new,return,switch,this,throw,try,typeof,var,void,while,with},
morekeywords=[2]{class,enum,export,extends,import,super,implements,interface,let,package,private,protected,public,static,yield},
morekeywords=[3]{prototype},
morestring=[b]{"},
morestring=[b]{'},
morestring=[b]{/},
morecomment=[l]{//},
morecomment=[s]{/*}{*/}}

\begin{lstlisting}[language=JavaScript]
function foo()
{
var regex = /abc/;
var ratio = 1.0/2.0;
return 0;
}
\end{lstlisting}

\end{document}

• Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. Mar 11, 2014 at 8:43
• @Jubobs OK, I provided a MCE. Mar 11, 2014 at 8:58

I can't think of a way that would allow you to do that without altering the original listing. What you can do is define custom delimiters that apply string style to their content, using

moredelim=[is][stringstyle]{<opening-delim>}{<closing-delim>}


The i in [is] means that those delimiters don't get printed in the output. Then surround all your regexp with those delimiters. That is kind of tedious, but I don't think you have any other option.

\documentclass{article}

\usepackage{tikz}
\usepackage{listings}

\begin{document}

\lstset{
basicstyle=\footnotesize\ttfamily\bfseries\color{cyan!50!black},
keywordstyle=\color{green!50!black},
keywordstyle=[2]\color{red!50!black},
stringstyle=\color{yellow!80!black},
identifierstyle=\color{blue!80!white},
escapeinside={(*@}{@*)}}

\lstdefinelanguage{JavaScript}{
morekeywords={break,case,catch,continue,debugger,default,delete,do,else,finally,
for,function,if,in,instanceof,new,return,switch,this,throw,try,typeof,var,void,while,with},
morekeywords=[2]{class,enum,export,extends,import,super,implements,interface,let,package,private,protected,public,static,yield},
morekeywords=[3]{prototype},
morestring=[b]{"},
morestring=[b]{'},
morecomment=[l]{//},
morecomment=[s]{/*}{*/},
moredelim=[is][stringstyle]{@<}{>@}, %<--- custom, invisible delimiters for highlighting unquoted'' strings
}

\begin{lstlisting}[language=JavaScript]
function foo()
{