23

I've noticed today that there's a lang-regex option for the syntax highlighter listed on What is syntax highlighting and how does it work?

I tried adding it to some of my regex snippets. I was expecting something close to the highlighter in regex101 or similar sites (minus the explanations), but all it does is color the snippet red. bummer.

I don't think it is reverting to the default, because in that case it would color the snippet white instead. I think. Maybe.

So, my question is: did this highlighter ever work? If it's some bug with it, can it be fixed?

Note that I'm not asking whether this highlighter should be the default for the tag. I already know this is not a good idea.

11
  • 1
    "If it's some bug with it, can it be fixed?" Stack Overflow uses Google Prettify, if I recall, so... If it's a bug, fixing it is a question for that project. Just as a side note.
    – Kendra
    Commented Sep 20, 2018 at 20:34
  • Thanks! I'm aware. Some languages are implemented as extensions, though. I'm also pretty certain Google Pretiffy is dead, so I'm not expecting any bug fix from its maintainers anytime soon. I was also thinking this may be some configuration issue. But I really don't know, hence the question. Commented Sep 20, 2018 at 20:38
  • Reading again, I misinterpreted what extensions mean. Bummer, again. Commented Sep 20, 2018 at 20:40
  • 7
    Seems my previous answer is wrong: regex syntax highlighting indeed does not work. Usually, a number of classes get added to a code block with syntax highlighting, and none get added on regex blocks. Waiting for an official answer. Note that the prettify GitHub has removed references for regex as a supported language.
    – Erik A
    Commented Sep 20, 2018 at 21:05
  • Some programming languages require to escape their strings differently. So the regex "a-z"\n+ in some contexts has to be written \"a-z\"\\n+ in some others. Highlighting regexes consequently needs to be done in two flavours (the escaping one and the non-escaping one).
    – Cœur
    Commented Sep 21, 2018 at 12:50
  • 31
    I tried to syntax highlight my regex and now I have three problems.
    – Lundin
    Commented Sep 21, 2018 at 14:03
  • Perhaps, someone could implement random coloring, like the syntax highlighter does for the other languages…
    – Holger
    Commented Sep 21, 2018 at 14:15
  • 3
    @Lundin xkcd.com/1313
    – LWChris
    Commented Sep 21, 2018 at 15:16
  • @Cœur correct, but many answers (I would dare say most, but I have no data to back it up) tend to just include the plain regex, ignoring concerns such as escaping the string to keep the answer simple. When we escape the string in the answer, it's mostly because the question had an error related to incorrect escaping already. Commented Sep 21, 2018 at 15:44
  • So a regex highlighter don't really need to concern itself with the underlying languages' rules. It could be argued, though, that different regex dialects could need different flavours of highlighting, but my experience with regex debugging tools has proven (for me) that this is not an issue. Commented Sep 21, 2018 at 15:47
  • @LWChris xkcd.com/1171
    – 0xDBFB7
    Commented Sep 22, 2018 at 7:22

1 Answer 1

9

If you look at the source code, the "regex" language just says "everything is a string:"

registerLangHandler(
      createSimpleLexer([], [[PR_STRING, /^[\s\S]+/]]), ['regex']);

Some time ago I noticed, like you did, that "regex" was a supported language. I then tried editing it into a regex question I had already asked, but was surprised when every character was red. That's when I looked into the source code and ultimately abandoned the edit.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .