Regex should be working but not in PrismJS?
See original GitHub issueI 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;
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:
- Created 8 years ago
- Comments:7 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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:This will match
var x
It will match
var y
It will match
var x
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.I hope it is more clear.
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