<story><title>Microlight.js, a code highlighting library</title><url>https://asvd.github.io/microlight/</url></story><parent_chain><item><author>Klathmon</author><text>Somewhat off topic, but is there a &amp;quot;regex alternation optimizer&amp;quot; out there? And would something like that be worth it?&lt;p&gt;I&amp;#x27;ve looked through some textmate-style syntax highlighting packages (used in sublime and in github&amp;#x27;s atom and probably others), and most of them need big (or somewhat big) sets of alternations for a bunch of keywords, and more often than not they are just set up as a list of full keywords with no thought to order or size.&lt;p&gt;Combining them into something like the below should theoretically be faster while also taking up less space (which is important in web libraries), and I feel like it wouldn&amp;#x27;t even be all that difficult.&lt;p&gt;&lt;pre&gt;&lt;code&gt;    de(bugger|cimal|clare|f(ault|er)?|init|l(egate|ete)?)
&lt;/code&gt;&lt;/pre&gt;
Is there something out there which can do this, and would it even be worth it or is this something best left to the JIT&amp;#x2F;optimizer of the regex engine?</text></item></parent_chain><comment><author>julian37</author><text>I&amp;#x27;m not aware of any tools out of the box, but you could trivially build your example regex from a Trie which is also easy to construct.&lt;p&gt;&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;en.wikipedia.org&amp;#x2F;wiki&amp;#x2F;Trie#Algorithms&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;en.wikipedia.org&amp;#x2F;wiki&amp;#x2F;Trie#Algorithms&lt;/a&gt;&lt;p&gt;I&amp;#x27;m not so sure it would take up much less space though, if you take gzip compression into account. See for example here:&lt;p&gt;&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;github.com&amp;#x2F;google&amp;#x2F;closure-compiler&amp;#x2F;wiki&amp;#x2F;FAQ#closure-compiler-inlined-all-my-strings-which-made-my-code-size-bigger-why-did-it-do-that&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;github.com&amp;#x2F;google&amp;#x2F;closure-compiler&amp;#x2F;wiki&amp;#x2F;FAQ#closure-...&lt;/a&gt;</text></comment>
<story><title>Microlight.js, a code highlighting library</title><url>https://asvd.github.io/microlight/</url></story><parent_chain><item><author>Klathmon</author><text>Somewhat off topic, but is there a &amp;quot;regex alternation optimizer&amp;quot; out there? And would something like that be worth it?&lt;p&gt;I&amp;#x27;ve looked through some textmate-style syntax highlighting packages (used in sublime and in github&amp;#x27;s atom and probably others), and most of them need big (or somewhat big) sets of alternations for a bunch of keywords, and more often than not they are just set up as a list of full keywords with no thought to order or size.&lt;p&gt;Combining them into something like the below should theoretically be faster while also taking up less space (which is important in web libraries), and I feel like it wouldn&amp;#x27;t even be all that difficult.&lt;p&gt;&lt;pre&gt;&lt;code&gt;    de(bugger|cimal|clare|f(ault|er)?|init|l(egate|ete)?)
&lt;/code&gt;&lt;/pre&gt;
Is there something out there which can do this, and would it even be worth it or is this something best left to the JIT&amp;#x2F;optimizer of the regex engine?</text></item></parent_chain><comment><author>hk__2</author><text>Frak [1] does something like that and was written with syntax higlighters in mind.&lt;p&gt;[1]: &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;github.com&amp;#x2F;noprompt&amp;#x2F;frak&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;github.com&amp;#x2F;noprompt&amp;#x2F;frak&lt;/a&gt;&lt;p&gt;edit: typo</text></comment>