2014-10-09 75 views
0

我使用這個代碼來突出我的「的Lua」代碼:錯誤的語法高亮js文件

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']; 

我看到這個錯誤控制檯:

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

請幫我解決這個錯誤。 謝謝。

回答

0

您收到的錯誤是由於以下的正則表達式所產生:

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

令牌需要以匹配字面上它們各自的字符進行轉義。也就是說,如果要匹配foo$bar,則應使用foo\$bar,因爲$令牌表示字符串的開頭。因此,所產生的正則表達式應該是:

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

我從來沒有使用稱作GeSHi或其SyntaxHighlight擴展之前,但我最好的猜測是使用下列內容:

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

嗨,我的問題與此解決。但突出顯示時會引發新問題。 [Here](http://www.filetolink.com/0ce70138fc)是一個包含LUA代碼的html文件,我試圖用SyntaxHighlight來突出顯示它們,但它錯誤地突出顯示。請看看並幫助我。 'operators' var中的符號必須以紅色突出顯示。謝謝,我在等待...... – 2016-04-08 18:42:40