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.

Research: more nuanced RegEx highlighting

See original GitHub issue

Is your request related to a specific problem you’re having?

I have a block of code with a rather long regex. The entire regex is one color.

The solution you’d prefer / feature you’d like to see added

Break a regex into smaller components, maybe with an enclosing .hljs-regexp class. Those components might be general, or regex-specific.

Additional context

(Theme: Nord) In Highlight.js image In VS Code image

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
joshgoebelcommented, Oct 15, 2020

This would likely fall under https://github.com/highlightjs/highlight.js/issues/2500.

maybe with an enclosing .hljs-regexp class.

Yes, what would be needed is actual subclasses (ie, regex.subgroup) (in a parser sense, not necessary CSS)… such as [forgive my naming]:

  • sub grouping ()
  • character group []
  • count modifier (*,?,+)
  • escape sequences?
  • pins (^, $)

We don’t have a way to codify such subclasses yet, but I’m open to figuring that out - it just hasn’t become necessary yet.

My worry here is this would require pretty accurate almost real parsing of regex (which traditionally we try to avoid because of the complexity and maintenance) - and that may or may not be possible. If someone came up with a rough prototype I’d definitely review it. But if the parsing can’t be fully accurate (ie, it misidentifies things regularly or simply can’t handle some regex) then I’m not very interested in trying to fix regex parsing bugs in perpetuity. And of course then themes would need to be altered to support this on a case by case basis unless the missing rules could be added programmatically.

ALSO worth noting: This could easily be done with a 3rd party plugin if it’s decided this does not belong in core. You’d simply redefine hljs.REGEXP_MODE to be whatever you desire [adding the rich detail you’re requesting vs custom css classes] - and then be responsible for providing a CSS theme that provided the new styles (or make that a task for the user). Almost all the grammars defer to REGEXP_MODE for highlighting regex, so by improving this rule all grammars would gain the new detailed highlighting.

So either way seems perhaps something worth exploring.

1reaction
joshgoebelcommented, Jan 7, 2021

I’m going to add “help welcome” but this really needs a champion who is willing to invest some serious time to work thru all the questions we’d need to resolve to make something mergeable and of benefit to all themes. If someone wanted to do something smaller in scope for themselves I’d recommend writing a plugin-extension that just modified the libraries regex mode (and then added their own custom CSS classes for whatever theme they were using) - which would automatically cover many grammars (but not all).

If no one steps up this will auto-close in a few months as I don’t see a need to leave it open forever.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using RegEx to Discover Sensitive Data - Netwrix Blog
Any data discovery and classification solution heavily relies on regular expressions (sometimes called RegExes, REs or RegEx patterns) to ...
Read more >
Idea: RegEx Highlighting - Relativity Community
Got anyone the highlighting of RegEx based terms working? I have the impression that is not working in Relativity 9.6, at least on...
Read more >
On the Impact and Defeat of Regular Expression Denial of ...
My novel regex engine optimizations seem the most promising approach for protecting existing regex engines, offering significant time reductions ...
Read more >
Regular expression for highlighting words - Stack Overflow
The first part of the query is indeed to walk over matches say "$+[0] - $-[0]" while $string =~ /abc/g;. and then the...
Read more >
Regex - Syntax error - Salesforce Stack Exchange
This is my regex (for international phone number), i have the message "Syntax Error" with this part of regex highlighted : s*[)]?[-\s.]?[(]?
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