fix(ruby): properly highlight %r{} regex literals containing nested patterns
Summary
This fixes an issue where %r{}-style regular expressions in Ruby were not correctly highlighted when they contained nested patterns or complex escape sequences. Patterns like (\.{2}|\A/) would either break highlighting or be ignored entirely due to improper handling of nesting inside %r{}.
Resolves: https://github.com/highlightjs/highlight.js/issues/4281
Root Cause
Highlight.js previously treated %r{} regex literals as flat strings. This caused the parser to:
- Fail on nested constructs like
{2}(quantifiers) - Break on position anchors like
\A - Terminate the regex early or skip highlighting entirely
Fix
- Introduces a top-level
REGEXPmode to match%r{},%r(),%r[],%r!, etc. anywhere in Ruby code - Enables nested brace parsing inside
%r{}usingcontains: ['self'] - Removes
%rvariants from the legacyRE_STARTERS_REblock (which still handles/.../contexts) - Keeps other regex behavior intact and scoped
Screenshots
| Before | After |
|---|---|
101142167176
@.***
Jacques Erasmus @.***> 于 2025年7月4日周五 19:49写道:
@.**** commented on this pull request.
In src/languages/ruby.js https://github.com/highlightjs/highlight.js/pull/4286#discussion_r2185158611 :
@@ -227,6 +227,27 @@ export default function(hljs) { ] };
- const REGEXP = {
- className: 'regexp',
- contains: [
hljs.BACKSLASH_ESCAPE,SUBST,{begin: /\{/, end: /\}/,@joshgoebel https://github.com/joshgoebel this should now be fixed, I also added some tests to verify these cases.
— Reply to this email directly, view it on GitHub https://github.com/highlightjs/highlight.js/pull/4286#discussion_r2185158611, or unsubscribe https://github.com/notifications/unsubscribe-auth/BK2N76VR5FECLYCE47MX7X33GZS4NAVCNFSM6AAAAACAIM4ROGVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMZDSOBWHA3DINRWHA . You are receiving this because you are subscribed to this thread.Message ID: @.***>
@joshgoebel any updates here? I've addressed the comments you left previously. Please take a look/merge when you get a chance 🙇