Sublime Forum

Syntax highlight bug when regex contains /*

#1

This code is given improper syntax highlighting… Everything from the /* through the end of the file is shown commented out.

                var matches = trailing.match(/^\/(^/]*)\/*(^/]*)/)
                if(matches) {
                        ... 

Any idea how to fix this? Putting /* */ after the regex restricts the buggy commenting to just the last half of the regex but that’s poor workaround… other developers would be perfectly correct to delete nonsense like that.

Thanks for any ideas!

0 Likes

#2

Aha, the problem is that the regex highlighter doesn’t realize that / inside ] doesn’t need to be escaped.

These regexes are equivalent:

    /[ab/]/
    /[ab\/]/

But Sublime only correctly highlights the second one.

0 Likes

#3

[quote=“bronson”]Aha, the problem is that the regex highlighter doesn’t realize that / inside ] doesn’t need to be escaped.

These regexes are equivalent:

    /[ab/]/
    /[ab\/]/

But Sublime only correctly highlights the second one.[/quote]

This is a language file problem, and also one of those situations I consider “just because you can, doesn’t mean you should”. I am always explicitly escaping things like that for consistency in my code (I think it makes it very clear that way). I realize though that it is valid if the slash is not escaped in the ] block, but escaping it will always mean that less intelligent parsers and less experienced coders don’t get confused. It doesn’t mean I won’t come across someone else’s code were they don’t escape stuff like I do, and it would be nice if the highlighter was able to catch this stuff, but In order to do this proper, it takes quite a bit more complexity in the language file to recognize valid un-escaped slashes. And I am sure this issue exists in multiple languages all implementing stuff like this slightly different.

I realize opinion may differ on this…

0 Likes