LeaVerou/prism — pull request #331 by zeitgeist87 (2014-08-14 07:43:14+00:00)
Title: Allow callback function to check matches

Hi,

I am not sure if you like this patch. I just tried something out and it seemed to work, so I thought I'd share
it. But I am aware, that this could be against the core concept of Prism, so I wouldn't be too dissapointed if you reject it ;)

This patch adds the property checkMatch, which can be used to supply a
Javascript callback function to accept or reject the match found by the
regex pattern. The functions state is provided with the `this` object
and it is preserved through multiple calls. The function gets the
current `match` as its first parameter and the `lastIndex` its state
applies to with the second. This way it is possible, to reuse the state
and avoid repeating the same checks over and over again.

This feature can be used very effectively to solve the annoying bug of
comments that are inside of strings or comments inside of regular
expressions. This would be nearly impossible to solve completely with
regular expressions, but it can be easily solved with a simple function
and four boolean variables, which track if the current index is inside
or outside a single, double quoted string or a regex.

Because the state is preserved through multiple calls, there is no
global lookbehind problem. So the function effectively traverses the
input string only once.