我正在尝试使用CodeMirror简单模式 http://codemirror.net/demo/simplemode.html创建我自己的编辑器并突出显示一些自定义关键字。但是,它会突出显示这些单词在其他单词中的出现。这是我定义编辑器模式的代码:
CodeMirror.defineSimpleMode("simple", {
// The start state contains the rules that are intially used
start: [
// The regex matches the token, the token property contains the type
{regex: /["'](?:[^\\]|\\.)*?(?:["']|$)/, token: "string"},
{regex: /;.*/, token: "comment"},
{regex: /\/\*/, token: "comment", next: "comment"},
{regex: /[-+\/*=<>!]+/, token: "operator"},
{regex: /[\{\[\(]/, indent: true},
{regex: /[\}\]\)]/, dedent: true},
//Trying to define keywords here
{regex: /\b(?:timer|counter|version)\b/gi, token: "keyword"} // gi for case insensitive
],
// The multi-line comment state.
comment: [
{regex: /.*?\*\//, token: "comment", next: "start"},
{regex: /.*/, token: "comment"}
],
meta: {
dontIndentStates: ["comment"],
lineComment: ";"
}
});
当我在编辑器中输入内容时,这就是突出显示的内容。我希望前两个出现的样式能够被设置,但后两个则不会。
这个正则表达式显然有问题:
/\b(?:timer|counter|version)\b/gi
但我已经尝试了几种不同的方法,并且相同的模式在其他正则表达式测试器中可以正常工作。例子:https://regex101.com/r/lQ0lL8/33 https://regex101.com/r/lQ0lL8/33。有什么建议吗?
Edit #1:
在 codemirror 定义中尝试了这种模式,删除了 /g 但它仍然会产生相同的错误突出显示。
{regex: /\b(?:timer|counter|version)\b/i, token: "keyword"}