If the comment does not contain a '
, that is to say that the last '
in the line is the beginning of any comment then it would seem to be best to work backwards from the end of the line, with a non-greedy search.
Try this, which uses your strings term but with a different comments term:
# Strings
color brightmagenta ""(\\.|[^"])*""
Comments.
color green "[^']*$"
This works, except that the comment mark ('
) is not coloured, unfortunately:

Adding in an additional '
, like so:
# Strings
color brightmagenta ""(\\.|[^"])*""
Comments.
color green "'[^']*$"
finally gives the desired result:

Note: The alternative to the non-greedy [^']*
is '.*?
, which is what I first tried to use, see below, but it failed...
Lazy (non-greedy) *?
causes an error
My first thought was to use .*?
, which would also colour the comment marker ('
), like so:
# Comments.
color green "'.*?$"
Logically, this non-greedy search from the end should also work for your comments.
However, nano throws an error:
Bad regex "'.*?$": repetition-operator operand invalid
It might be a bug, see Bad regex in php.nanorc, sh.nanorc & zsh.nanorc using nano 2.3.6 on Mac OS X from brew #41 which was supposedly fixed:
Answer: POSIX regex doesn't support ? for laziness (or laziness at all). Removing the ?s from the offending lines got rid of the errors, but you may not want to do that here since it will change evaluation for everyone else.
Note: I got this error when running nano on OS X - I'm not sure if that is relevant.