I am trying to use listings
to display Javascript code. Following the replies to another question, I worked out the following code:
\documentclass{article}
\usepackage{listings}
\lstdefinelanguage{JavaScript}{
keywords={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={class, export, boolean, throw, implements, import, this},
comment=[l]{//},
morecomment=[s]{/*}{*/},
string=[b]",
morestring=[b]',
sensitive=true
}
\begin{document}
\begin{lstlisting}[breaklines={true}, language={Javascript}, numbers=left]
var string = "This is special' string";
var alpha = document.selection;
str = str.replace(/[!.:?,;\"]/g, '');
dictionary_access ("" + t );
var alpha = document.selection;
\end{lstlisting}
\end{document}
However the displayed listing does not have proper formatting (whitespaces being highlighted outside the string). I guess it is because the string delimiters are not escaped properly.
Someone else also had a similar issue with Basic. I have tried replacing [b] in string=[b]"
with [bd]
, [m]
for both single and double quote. Also tried using string=[s]""
but no success.