Javascript 代码镜像:使用正则表达式
我试图在代码镜像中使用正则表达式作为简单模式 我测试它的最低代码:Javascript 代码镜像:使用正则表达式,javascript,regex,codemirror,codemirror-modes,Javascript,Regex,Codemirror,Codemirror Modes,我试图在代码镜像中使用正则表达式作为简单模式 我测试它的最低代码: CodeMirror.defineMode("regex", function() { return { token: function(stream, state) { console.log(stream); a = stream.match(/word/); console.log(a); stream.skipToEnd(); return null; }
CodeMirror.defineMode("regex", function() {
return {
token: function(stream, state) {
console.log(stream);
a = stream.match(/word/);
console.log(a);
stream.skipToEnd();
return null;
}
};
});
第一遍的输出为:
Object { start: 74, pos: 74, string: "This is a sentence with word and key in it, and word and key are repeated.", tabSize: 4, lastColumnValue: 0, lastColumnPos: 0, lineStart: 0 } regex.js:5
null
如果我使用字符串字而不是正则表达式,它将记录undefined而不是null
代码镜像的文档说明功能匹配:
模式可以是字符串或以开头的正则表达式^
我不清楚的是,regex的“not”是什么意思
这是我第一次使用codemirror、正则表达式和javascript,所以我可能遗漏了一些明显的东西。我认为这是^word。^称为起始锚点,指行的开始,$指行的结束。如果^出现在字符类[]的开头,则它表示列表中给定字符的求反
示例:[^:]-匹配任何字符,但不匹配以下字符:好的,明白了
a = stream.match(/word/);
在流的当前位置检查正则表达式,即流是否位于以下位置的开头:
"This is a sentence with word and key in it, and word and key are repeated."
然后它将只检查第一个字母,将在T处停止,因为它与正则表达式不匹配,并返回null
因此,在不满足正则表达式的情况下推进流是有意义的,这就解释了为什么使用^if adviced。我认为它是^word