Atom Grammar problem recognizing square brackets

I quite desperate with the following problem. I am trying to modify hyperlink.cson grammar file (https://github.com/atom/language-hyperlink/blob/master/grammars/hyperlink.cson) in order to recognize squared brackets in URLs; e.g., URLs such as http://aaa.com[fsdf]. I already modified it successfully to recognize normal parenthesis such as http://aaa.com(fsdf). However, I cannot make it work with the brackets. For example, I tried with the following expression:

match: \b((https?|thunderlink|cbthunderlink|imap-message|mailbox-message|s?ftp|ftps|file|smb|afp|nfs|(?:x-)?man(?:-page)?|gopher|txmt|issue):\/\/| (www.))((?!(\\#[[:word:]]*\\#))(?:[-:@[:word:].~%$+_\/?=&#;\|\!\(\)\[\]]))+(?<![-.,?:#;])

This expression works perfectly well in https://regex101.com but it does not work at all in Atom. Indeed, the expression without the part \[\] seems to work but after adding it (to try to recognize square brackets) all the highlights stop working.

I even tried with a simpler expression, which just tries to recognize []:

match: "[\[\]]"

but this does not work either.

I’ve spent a lot of time with this and I cannot find the solution. Maybe I am missing something. Any idea is very welcome.

1 Like

maybe

match: "[\\[\\]]"

In theory that should be correct, but I cannot manage to make it work. If I add the following to hyperlink.cson (or something similar), it does not work:

  {
    match: "[\[\]]"
    name: "markup.underline.link.$1.hyperlink"
  }

I think you need to double escape things,
one time for the string, another for the regex parsing

So

{
   comments: "[\\[\\]] is different from [\[\]]"
   match: "[\\[\\]]"
   name: "markup.underline.link.$1.hyperlink"
}

I definitely cannot manage to make it work. Probably no need to double scape, as the following one works:

{
match: “(?x)\b((https?|thunderlink|cbthunderlink|imap-message|mailbox-message|s?ftp|ftps|file|smb|afp|nfs|(?:x-)?man(?:-page)?|gopher|txmt|issue)://| (www.))((?!(\#[[:word:]]*\#))(?:[-:@[:word:].~%$+_/?=&#;|!()]))+(?<![-.,?:#;])”
name: “markup.underline.link.$1.hyperlink”
}

Hmm, that’s weird, because the javascript grammar uses double escapes

(I chose a line with a lot of double escapes…)