Search code examples
javascriptprismjs

Prism.js highlight only if it begins the line


I'm writing a custom language extension to Prism.js and have a problem highlighting comments. I want to highlight comments, that begin with either # or // and start at the beginning of the line:

# Example comment
// Example comment
1*2//comment <-- this should not be highlighted

Here is the pattern I'm using to detect comments: /(^|[^\\])[#\/\/].*/.

Unfortunately, it doesn't work as expected:

demo

I tried using the lookbehind and greedy options (docs), but it didn't help.


Solution

  • Fixed by changing the regular expression to /(^|^\\)(\/{2}|#).*/gm