0

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.

Mahdi.s
  • 3
  • 5

1 Answers1

0

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 = '~ ! @ # \\$ % \\^ & \\* \\( \\) - \\+ = \\. \\/ ; \\? { }';
Spooky
  • 2,966
  • 8
  • 27
  • 41
  • Hi, My Problem solved with this. but caused a new problem when highlighting. [Here](http://www.filetolink.com/0ce70138fc) is a html file contains LUA code that I trying to highlight them with SyntaxHighlight but it wrongly highlighted. Please see it and help me. The symbols in `operators` var must be highlight with RED color. Thanks, I'm Waiting... – Mahdi.s Apr 08 '16 at 18:42