Incorrect RegExp detection in JavaScript

Issue #403 resolved
Anonymous created an issue

In !JavaScript, a single forward slash following one of {{{ [({=,:;!%^&*|?~+- }}} starts a regular expression. Spaces are not required. For example, this is valid syntax: {{{ if (!/regexp/.test(foo)) }}} but this is not recognized by Pygments.

See more examples at http://pygments.org/demo/1605/

Reported by Pumbaa80

Comments (7)

  1. Anonymous

    I had a look at [http://dev.pocoo.org/projects/pygments/browser/pygments/lexers/web.py web.py] and I'm afraid there are [http://pygments.org/demo/1621/ some more issues] with !JavascriptLexer.

    First, according to [http://interglacial.com/javascript_spec/a-7.html#a-7.7 the JavaScript spec], this should be the right way to match operators and punctuators:

    (r'=|++|--|~|&&|\\?|:|\\|\\||\\\\|(<<|>>>?|==?|!=?|[<>+-*%&\\|\\^/])=?', Operator)
    (r'[{}()\\[\\].;,]', Punctuation)
    

    Second, character classes need a special treatment... (use several states?)

    Once this is fixed, the detection of a regexp literal's end (cf. lines 47 to 49 in web.py) can be simplified/fixed. This should sufficiently replace the three lines:

    ...   /([gim]+\\b|\\B)', String.Regex)
    

    !ActionScriptLexer and !ActionScript3Lexer might need a fix, too (?).

  2. Log in to comment