如何以字符串样式突出显示独立的正则表达式?

如何以字符串样式突出显示独立的正则表达式?

我正在使用列表来格式化一段 JavaScript 代码,但在以下代码中遇到了问题:

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

我希望正则表达式突出显示为字符串,但如果我使用以下定义,除号将被识别为字符串的开头:

\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]{/*}{*/}
}

我该如何解决这个问题?我能以某种方式摆脱除法符号吗?


来源:

\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},
commentstyle=\itshape\color{gray!50!white},
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()
{
    // comments
    var regex = /abc/;
    var ratio = 1.0/2.0;
    return 0;
}
\end{lstlisting}

\end{document}

答案1

我想不出有什么方法可以让你做到这一点而不改变原始列表。你可以做的是定义自定义分隔符,将字符串样式应用于其内容,使用

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

in表示这些分隔符不会打印在输出中。然后用这些分隔符包围所有正则表达式。这有点乏味,但我认为你没有其他选择i[is]

在此处输入图片描述

\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},
commentstyle=\itshape\color{gray!50!white},
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()
{
    // comments
    var regex = @</abc/>@;
    var ratio = 1.0/2.0;
    return 0;
}
\end{lstlisting}

\end{document}

相关内容