I have a script that outputs code with lots of ASCII escape sequences pertaining to color (for example, \033[91m changes the color of the text to red when printed to a terminal). I’d like to try to output this to HTML, so I tried this:
str = str.replace(new RegExp('\033[91m', 'g'),
'<span style="color:#ff0011">')
/*.replace more such codes*/;
However, I get the following error:
SyntaxError: Invalid regular expression:
/[91m/: Unterminated character class
How can I fix that?
Change it to
'\033\\[91m'. I.e. you have to escape the'['so that it doesn’t think you are giving it a list of characters to match in that position.