JavaScript 中的语法高亮
我目前正在研究 Javascript 中的一些语法突出显示。
为了匹配字符串,我会使用这样的东西:code = code.replace(/("([^"\\]*(\\.[^"\\]*)*)")/gm, "$1"); // string
这将匹配一个整数:code = code.replace(/(\d+)/gm, "$1");
现在我的问题是一个整数在字符串中也会被匹配。这不是突出显示的问题,而是性能的问题,因为我可以使用
<代码>代码> span.number 而不是代码 span.number
。
对于防止这种行为有什么建议吗?
我还通读了 TextMate 的语言语法,这对我来说似乎非常强大。但是,我不知道如何在 Javascript 中实现它。
一些帮助将不胜感激。
我知道有很多好的语法可以突出显示周围的内容,但它们都没有为 Objective-C 提供真正好的支持。
I am currently working on some syntax highlighting in Javascript.
To match strings, I would use something like this:code = code.replace(/("([^"\\]*(\\.[^"\\]*)*)")/gm, "<span class=\"string\">$1</span>"); // string
This would match an integer:code = code.replace(/(\d+)/gm, "<span class=\"number\">$1</span>");
Now my problem is that an integer within a string would get matched too. It's not a problem of highlighting but of performance, since I could usecode > span.number
instead of code span.number
.
Any suggestions for preventing this kind of behavior?
I also read through TextMate's Language Grammars which seem really powerful to me. However, I have no idea how I could implement that in Javascript.
Some help would really be appreciated.
I know that there are many good syntax highlighting things around, but none of them provides really good support for Objective-C.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想为 Google Code Prettify 编写 Objective-C 模式,我会很高兴将其纳入其中。
您可以在 http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-ml.js
这样的行
表示这是一个突出显示规则对于一个字符串和一个字符串匹配上面的正则表达式(双引号内的一系列非引号或转义序列或单引号内的一系列非单引号或转义序列)。
该规则
匹配数字文字。数字文字是一个可选的符号,后跟十六进制文字、一个点后跟一个分数或一个整数部分后跟一个分数之一,最后两个可以在末尾有一个可选的指数。
代码美化引擎负责确保字符串被视为字符串,并且字符串中看起来像数字的东西不会被误认为是数字。
If you want to write an objective-C mode for Google Code Prettify I would be happy to incorporate it.
You can see an example mode for OCAML/SML style languages at http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-ml.js
Lines like
say that this is a highlighting rule for a string and a string matches the regular expression above (a run of non-quotes or escape sequences inside double quotes or a run of non-single-quotes or escape sequences inside single quotes).
This rule
matches number literals. A number literal is an optional sign followed by one of a hex literal, a dot followed by a fraction, or an integer part followed by a fraction, and the last two can have an optional exponent at the end.
The code prettify engine takes care of making sure that strings are treated as strings and things that look like numbers inside strings are not mistaken for numbers.
也许您想看看 Alex Gorbachev 的 SyntaxHighlighter。目前没有针对 Objective C 的模块,但他提供了手册用于开发“自定义画笔” ”。
Maybe you would want to take a look at Alex Gorbachev's SyntaxHighlighter. Theres currently no module for Objective C but he provides a manual for developing "custom brushes".