CodeMirror 突出显示特定的正则表达式匹配

CodeMirror Highlight specific Regex-Match

我试图在 htmlmixed 模式下突出显示所有 %()% 子字符串。匹配的 RegExp 是 ([%(](.*)[)%]).

这是我用于 CodeMirror 的代码:

const code = CodeMirror.fromTextArea(document.querySelector("#id"), {
     theme: "dracula",
     mode: "text/html",
     lineNumbers: true,
     firstLineNumber: 1,
     spellcheck: false,
     autocorrect: true,
     extraKeys: { "Ctrl-Space": "autocomplete" },
     styleActiveLine: true,
     highlightSelectionMatches: { showToken: /\w/, annotateScrollbar: true }
});

谢谢

您必须在 highlightSelectionMatches 中添加样式 属性。

const code = CodeMirror.fromTextArea(document.querySelector("#id"), {
     theme: "dracula",
     mode: "text/html",
     lineNumbers: true,
     firstLineNumber: 1,
     spellcheck: false,
     autocorrect: true,
     extraKeys: { "Ctrl-Space": "autocomplete" },
     styleActiveLine: true,
     highlightSelectionMatches: { 
          minChars: 2,
          showToken: /\w/,
          style:'matchhighlight',
          annotateScrollbar: true
    }
});

在css下面添加:

.cm-matchhighlight {
  background: red !important
}