question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Regex should be working but not in PrismJS?

See original GitHub issue

I have this:

Prism.languages.insertBefore("javascript", "comment", {
    "def": {
        pattern: /(var\s+|(?!^),\s*|(?!^),\s*^|[^\\:]\/\/.*\n\s*)((?!\d)[a-z0-9_$]+)(?= *[=,;])/,
        lookbehind: !0
    }
})

This should highlight all variable identifiers in the declaration code block.

It works on Regex101: https://regex101.com/r/uF4oY4/46

Why doesn’t it work with this? The y in var x, y and y = undefined aren’t highlighted.

var x;
var y;
//or
var x, y; //with multiple variables declared

var x,
    y = undefined;

capture

The y isn’t wrapped in any class.


P.S. I know, it falsely highlights this, but that’s not the point:

x, y;

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Golmotecommented, Aug 16, 2015

The problem is related to the parts (?!^) in your regexp. Prism breaks the string into pieces progressively as it matches patterns, so here are the strings that are tested, in order:

  1. First, Prism will test the entire text for your `def` pattern:
    var x;
    var y;
    //or
    var x, y; //with multiple variables declared
    
    var x,
        y = undefined;
    

    This will match var x

  2. Then Prism will test the following string. Notice how the first match has been removed.
    ;
    var y;
    //or
    var x, y; //with multiple variables declared
    
    var x,
        y = undefined;
    

    It will match var y

  3. Then we have this:
    ;
    //or
    var x, y; //with multiple variables declared
    
    var x,
        y = undefined;
    

    It will match var x

  4. Then we have this:
    , y; //with multiple variables declared
    
    var x,
        y = undefined;
    

    And here is the problem: your regexp can’t match because of the (?!^) clauses that fail. Indeed, the way Prism works, the comma is actually at the beginning of the string.

  5. I hope it is more clear.

0reactions
Golmotecommented, Aug 17, 2015

Well… CodeMirror reads the code character by character to determine the tokens. This way of doing things allows it to understand the code. See the code source of the JS mode in CodeMirror: http://codemirror.net/mode/javascript/javascript.js.

Prism, on the other side, uses regexps to determine the tokens. So it is not aware of the actual meaning of the code. This was a design choice made by @LeaVerou, to allow for easier addition of new languages and small code size. See the code source of the JS component in Prism: https://github.com/PrismJS/prism/blob/gh-pages/components/prism-javascript.js

Read more comments on GitHub >

github_iconTop Results From Across the Web

Prismjs: regex positive lookbehind equivalent? - Stack Overflow
@WiktorStribiżew It seems that it supports positive/negative lookahead, but not positive/negative lookbehind. Is it impossible to make positive ...
Read more >
FAQ - Prism.js
Prism works fine in Opera. However, this page might sometimes appear to not be working in Opera, due to the theme switcher triggering...
Read more >
Extending Prism
Most tokens in most languages are not greedy, because they don't need to be. Typically only the comment, string, and regex literal tokens...
Read more >
Known failures - Prism.js
There are certain edge cases where Prism will fail. There are always such cases in every regex-based syntax highlighter. However, Prism dares to...
Read more >
Prism.js
Dead simple Include prism.css and prism.js, use proper HTML5 code tags ( code.language-xxxx ) ... Add new features thanks to Prism's plugin architecture....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found