Fixing invalid regular expression error with highlight.js and Next.js production build

Piotr Pliszko

For some time already, I've encountered a recurring issue with highlight.js on my blog's production build. What's interesting is that the issue never occurs on dev server, so I can only catch it when testing prod build.

Then, trying to visit the problematic post, I got this (or similar) error in the console:

SyntaxError: Invalid regular expression: /\0[oO](([0-7]_*)+)\/mu: Invalid escape

It's always some error related to "invalid regular expression", without any other specific information.

The issue turned out to be a problem with highlight.js library, which had some problems parsing one of the code blocks.

After a little bit of investigation, I found this thread on GitHub that pointed me to the root cause - Haskell grammar was a problem. I am not sure why it was throwing an error on non-related code blocks, but it's probably due to how the library works internally.

First, I've tried a tip with transpiling highlight.js, but with no success. Since I don't have any Haskell code on my blog (at least for now!), I've decided to try another way - just disabling Haskell syntax highlighting.

In order to do this, I've just added:

hljs.unregisterLanguage('haskell');
hljs.unregisterLanguage('hs');

This solved the issue - all other code blocks are correctly highlighted.

If you have any questions or feedback, feel free to reach out to me on GitHub, X , Bluesky or LinkedIn!