1. Georg Brandl
  2. pygments-main
  3. Issues
Issue #356 resolved

Javascript lexer overzealous about highlighting regexps

thatch
created an issue

Here are two examples.

{{{ var x = 1/(1+Math.sqrt(sum)) // convert to number between 1-0 return Math.round((num / den) * 100)/100 }}}

Comments (5)

  1. thatch reporter

    I propose a quick heuristic:

    • `/` followed by `[gsmi]
      b` is a regex with suffix
    • `/` followed by `
      s*[,);]` is the end of a regex.
    • `/` followed by `
      s*
      .[a-z]` is the end of a regex with part of a method call.
    • `/` followed by `
      s*[0-9(]` is definitely not the end of a regex.

    These would stay regexes:

    blah(/abc/);
    x = /abc/;
    x = /abc/.match;
    

    These specific misparses would be fixed:

    blah(1/2); //comment
    x = 1 / 2 / 3;
    x = 1/1/.1;
    

    These would still be broken:

    x=/1/;
        (treats =/ as an operator, while x is a regex in Firefox 3)
    x=1/a/g;
        (the g looks like a modifier, but this correctly is treated as math by Firefox 3)
    x=a/a/g;
        (same as above)
    

    The last two also are mishandled in vim. Without variable length lookbehind I don't see an easy way to fix these. See [febbbf8f29c2] for the fix, which I think is good enough to close this ticket.

  2. Log in to comment