2

I'm using the Rainbow.js library to add syntax highlighting to LaTeX code like so:

Rainbow.extend('latex', [
    {
        'name': 'comment',
        'pattern': /%.*$/gm
    },
    {
        'name': 'storage.function',
        'pattern': /\\[A-z@]+/g
    },
    {
        'matches':
        {
            1: 'entity.name.function',
            3: 'entity.class'
        },
        'pattern': /(\\(begin|end))\s*\{(.*?)\}/g
    }
], true)

but it fails to highlight group #3 even though—by everything I see otherwise—the group is being captured. Any idea what may be going wrong? Why would it match the first group but not the third?

Sean Allred
  • 3,558
  • 3
  • 32
  • 71
  • 2
    Not a direct answer to your question, but have you tried using non-capturing groups for the groups that don't need capturing? E.g. `(\\(?:begin|end))` (and then I guess you need `matches: {1: ..., 2: ...}` or `matches: {0: ..., 1: ...}`). – Felix Kling Aug 25 '14 at 22:41
  • @FelixKling Thanks for that! I'm still *very* new to JavaScript and extended regex in general. Shouldn't have an impact though, yes? – Sean Allred Aug 25 '14 at 22:42
  • `[A-z]` is possibly an invalid range (it is case-sensitive): You need `[A-Za-z]` – OnlineCop Aug 25 '14 at 22:43
  • @FelixKling O.O So adding that change did fix the issue. Would you like to post that as an answer? – Sean Allred Aug 25 '14 at 22:43
  • @OnlineCop Thanks for the tip, fixed! – Sean Allred Aug 25 '14 at 22:44
  • Well, since I'm not familiar with Rainbow.js, I'd rather have someone write an answer who can explain why that fixes it. – Felix Kling Aug 25 '14 at 22:44
  • `[A-z]` is valid, but captures a bit more than letters (e.g. square brackets). And @FelixKling is right, rainbow apparently only colours sequential `matches`, and stops when it gets `undefined` for `2`. – Amadan Aug 25 '14 at 22:44
  • @FelixKling Definitely fair. I'm curious myself… **Edit** I had actually figured as much :) – Sean Allred Aug 25 '14 at 22:44

1 Answers1

0

Your problem is that you forgot to escape a second bracket so it would be /(\\(begin|end\\))\s*\{(.*?)\}/g.

But you will only have two groups the first (\\(begin|end\\)) and the second (.*). If you don't need to get the content of a group you can make it non-capturing (it will improve the performance by the way) see What is a non-capturing group? What does a question mark followed by a colon (?:) mean?.

For the A-z range it doesn't work like a-zA-Z see character classes. You should rather use a a-z range with the flag i which means you ignore case.

Community
  • 1
  • 1
Gadcam
  • 48
  • 6