Skip to content

Regex look-ahead expressions do not work properly (begin) #1696

@jandamm

Description

@jandamm

This issue is somewhat related to #1550 where I commented too.
But I decided to give a better/clearer example how to reproduce the issue.

The problem is that grouped regexes are combined with into one regex using | (OR). After the match, it is not checked if the match really matches to the current group.

To reproduce this issue, you can use this parser and replace the swift.js one with it.

module.exports = function(hljs) {
  const VAR1 = { 
    className: 'variant_1',
    begin: /\b[a-z][A-Za-z0-9]*\b(?!\()/g
  };
  const VAR2 = {
    className: 'variant_2',
    begin: /\b[a-z]/g
  };
  return { contains: [{
    begin: /[A-Za-z0-9]\./, end: /[\.|\,|\)|\n]/,
    contains: [VAR1, VAR2]
}]};}

Now you match this code:

<pre><code class="language-swift">
  a.make)
  a.bake()
</code></pre>

And it will turn into this:

<pre><code class="language-swift">
  a.<span class="hljs-variant_1">make</span>)
  a.<span class="hljs-variant_1">b</span>ake()
</code></pre>

But actually m matches variant_2.

If you remove VAR2 from the contains array it works fine. (make)
Same goes if you remove VAR1. (m, b)

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions