Error in syntax highlighter js file

Issue

I’m using this code to highlight my “Lua” codes:

SyntaxHighlighter.brushes.Lua = function()
{
var keywords =  'break do end else elseif function if local nil not or repeat return and then until while this';
var funcs = 'math\\.\\w+ string\\.\\w+ os\\.\\w+ debug\\.\\w+ io\\.\\w+ error fopen dofile coroutine\\.\\w+ arg getmetatable ipairs loadfile loadlib loadstring longjmp print rawget rawset seek setmetatable assert tonumber tostring';
var operators = '~ ! @ # $ % ^ & * ( ) - + = . / ; ? { }';

this.regexList = [
    { regex: new RegExp('--\\[\\[[\\s\\S]*\\]\\]--', 'gm'),     css: 'comments' },
    { regex: new RegExp('--[^\\[]{2}.*$', 'gm'),                css: 'comments' },  // one line comments
    { regex: SyntaxHighlighter.regexLib.doubleQuotedString,     css: 'string' },    // strings
    { regex: SyntaxHighlighter.regexLib.singleQuotedString,     css: 'string' },    // strings
    { regex: new RegExp(this.getKeywords(keywords), 'gm'),      css: 'keyword' },   // keyword
    { regex: new RegExp(this.getKeywords(funcs), 'gm'),         css: 'func' },      // functions
    { regex: new RegExp(this.getKeywords(operators), 'gm'),    css: 'operator' },   // operators
    ];
}

SyntaxHighlighter.brushes.Lua.prototype = new SyntaxHighlighter.Highlighter();
SyntaxHighlighter.brushes.Lua.aliases = ['lua'];

I see this error in console:

Uncaught SyntaxError: Invalid regular expression: /\b(?:~|!|@|#|$|%|^|&|*|(|)|-|+|=|.|/|;|?|{|})\b/: Nothing to repeat 

Please help me to solve this error.
Thanks.

Solution

The error you are receiving is due to the following regular expression being generated:

\b(?:~|!|@|#|$|%|^|&|\*|(|)|-|\+|=|.|\/|;|\?|{|})\b

Tokens need to be escaped in order to match their respective characters literally. That is, if you want to match foo$bar, foo\$bar should be used, because the $ token represents the beginning of the string. Therefore, the generated regex should be:

\b(?:~|!|@|#|\$|%|\^|&|\*|\(|\)|-|\+|=|\.|\/|;|\?|{|})\b

I have never used GeSHi or its SyntaxHighlight extension before, but my best guess would be to use the following:

var operators = '~ ! @ # \\$ % \\^ & \\* \\( \\) - \\+ = \\. \\/ ; \\? { }';

Answered By – Spooky

Answer Checked By – Timothy Miller (AngularFixing Admin)

Leave a Reply

Your email address will not be published.