CodeMirror-将RegEx与覆盖一起使用 [英] CodeMirror - Using RegEx with overlay
问题描述
我似乎找不到使用RegEx匹配在CodeMirror中创建覆盖图的人的示例。一次匹配一件事的小胡子示例似乎很简单,但是在API中,它表示RegEx匹配返回匹配的数组,在小胡子示例中,我无法在结构的上下文中弄清楚该如何处理。
I can't seem to find an example of anyone using RegEx matches to create an overlay in CodeMirror. The Moustaches example matching one thing at a time seems simple enough, but in the API, it says that the RegEx match returns the array of matches and I can't figure out what to do with it in the context of the structure in the moustaches example.
我有一个正则表达式,该表达式查找需要突出显示的所有元素:我已经对其进行了测试,并且可以正常工作。
I have a regular expression which finds all the elements I need to highlight: I've tested it and it works.
我应该在令牌函数之外加载数组,然后匹配每个数组吗?还是有一种使用数组的方法?
Should I be loading up the array outside of the token function and then matching each one? Or is there a way to work with the array?
另一个问题是我想根据正则表达式中的(biz | cms)选项应用不同的样式-一个用于 biz,另一个用于 cms。会有其他人,但我正努力保持简单。
The other issue is that I want to apply different styling depending on the (biz|cms) option in the regex - one for 'biz' and another for 'cms'. There will be others but I'm trying to keep it simple.
这是我所掌握的。注释显示了我的困惑。
This is as far as I have got. The comments show my confusion.
CodeMirror.defineMode("tbs", function(config, parserConfig) {
var tbsOverlay = {
token: function(stream, state) {
tbsArray = match("^<(biz|cms).([a-zA-Z0-9.]*)(\s)?(\/)?>");
if (tbsArray != null) {
for (i = 0; i < tbsArray.length; i++) {
var result = tbsArray[i];
//Do I need to stream.match each element now to get hold of each bit of text?
//Or is there some way to identify and tag all the matches?
}
}
//Obviously this bit won't work either now - even with regex
while (stream.next() != null && !stream.match("<biz.", false)) {}
return null;
}
};
return CodeMirror.overlayMode(CodeMirror.getMode(config, parserConfig.backdrop || "text/html"), tbsOverlay);
});
推荐答案
查看代码镜像方法 match的实现(),您会看到它处理两种类型的方法参数:字符串和 RegExp 。
Look at implementation of Code Mirror method match() and you'll see, that it processes method parameter for two types: string and RegExp.
您的常量
stream.match("<biz.")
是字符串类型。
在 RegExp中定义类型:
tbsArray = /<biz./g
因此,您的流将与RegExp匹配。
Thus, your stream will be matched with RegExp.
这篇关于CodeMirror-将RegEx与覆盖一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!