为谷歌代码prettify编译正则表达式

Compiling regular expressions for google-code-prettify

本文关键字:编译 正则表达式 prettify 代码 谷歌      更新时间:2023-09-26

我们正在开发谷歌代码pretify的扩展,它为网页上的源代码着色。我们在Mathematica中有一个很长的关键字列表(大约4000个),虽然性能仍然很好,但我想知道我是否可以加快速度。

我们的关键字列表的正则表达式如下

var keywords = 'AbelianGroup|Abort|AbortKernels|AbortProtect|Above|Abs|Absolute|'
AbsoluteCurrentValue|AbsoluteDashing|AbsoluteFileName|AbsoluteOptions|'
AbsolutePointSize|AbsoluteThickness|AbsoluteTime|AbsoluteTiming|AccountingForm';
new RegExp('^(?:' + keywords + ')''b')

这样的-ed正则表达式在编译时能更快吗?首先编译它有意义吗?因为谷歌代码prettify是一个在服务器上运行的JavaScript。我不知道这个脚本是否每次加载网页时都是新加载的。在这种情况下,编译它可能不值得花费太多开销。

  1. google-code-prettify在客户端上运行(它是一个脚本;浏览器从服务器请求源)
  2. 创建RegExp对象会在运行时编译它

换言之,保持原样。