var DelphiKeywords = ‘absolute|abstract|and|array|as|asm|assembler|at|automated|’ + ‘begin|case|cdecl|class|const|constructor|contains|’ + ‘default|destructor|dispid|dispinterface|div|do|downto|dynamic|’ + ‘else|end|except|export|exports|external|’ + ‘far|file|final|finalization|finally|for|forward|function|’ + ‘goto|if|implementation|implements|in|index|inherited|initialization|inline|interface|is|’ + ‘label|library|message|mod|near|nil|nodefault|not|’ + ‘object|of|on|operator|or|out|overload|override|’ + ‘package|packed|pascal|private|procedure|program|property|protected|public|published|’ + ‘raise|read|readonly|record|reference|register|reintroduce|repeat|requires|resident|resourcestring|’ + ‘safecall|sealed|set|shl|shr|static|stdcall|stored|strict|string|’ + ‘then|threadvar|to|try|type|unit|until|uses|var|varirnt|virtual|’ + ‘while|with|write|writeonly|xor’; var CppKeywords = ‘__declspec|__exception|__fastcall|__finally|__published|__try|’ + ‘break|case|catch|char|class|const|const_cast|continue|’ + ‘default|delete|deprecated|dllexport|dllimport|do|double|dynamic_cast|’ + ‘else|enum|explicit|extern|false|float|for|friend|goto|if|inline|int|long|’ + ‘mutable|naked|namespace|new|noinline|noreturn|nothrow|’ + ‘private|protected|public|register|reinterpret_cast|return|’ + ‘selectany|short|sizeof|static|static_cast|struct|switch|’ + ‘template|this|thread|throw|true|try|typedef|typeid|typename|’ + ‘union|unsigned|using|uuid|virtual|void|volatile|whcar_t|while’; var CsKeywords = ‘abstract|as|base|bool|break|byte|case|catch|char|checked|class|const|continue|’ + ‘decimal|default|delegate|do|double|else|enum|event|explicit|extern|’ + ‘false|finally|fixed|float|for|foreach|from|get|goto|group|’ + ‘if|implicit|in|int|interface|internal|into|is|join|let|lock|long|’ + ‘namespace|new|null|object|operator|orderby|out|override|’ + ‘params|partial|private|protected|public|readonly|ref|return|’ + ‘sbyte|sealed|select|set|short|sizeof|stackalloc|static|string|struct|switch|’ + ‘this|throw|true|try|typeof|uint|ulong|unchecked|unsafe|ushort|using|’ + ‘value|var|virtual|void|volatile|where|while|yield’; var CsClass = ‘Serializable|Console|Convert|Object|DllImport’; //随时补充 var JsKeywords = ‘break|false|in|this|void|continue|for|new|true|while|delete|function|null|’ + ‘typeof|with|else|if|return|var|case|debugger|export|super|catch|default|’ + ‘extends|switch|class|do|finally|throw|const|enum|import|try’; //window.onload = function() { var pres = document.getElementsByTagName("PRE"); for (var i=0; i< pres.length; i++) { //Delphi if (pres[i].className == "Delphi") { str = pres[i].innerHTML; r1 = "(\\(\\*[\\s\\S]*?\\*\\))"; // (* *) r2 = "((?:http:)?//.*)"; // "//" and not "http://" r3 = "({(?!\\$)[\\s\\S]*?})"; // { } r4 = "(\\{\\${1}[a-zA-Z]+.+\\})"; // {$ } r5 = "(‘.*?’)"; // ‘ ‘ r6 = "([\\$\\#]{1,2}[a-fA-F0-9]+)\\b"; // $Hex or #Number r7 = "\\b(\\d+\\.?\\d*)\\b"; // Number r8 = "\\b(" + DelphiKeywords + ")\\b"; // Key rs = r1 + ‘|’ + r2 + ‘|’ + r3 + "|" + r4 + "|" + r5 + "|" + r6 + "|" + r7 + "|" + r8; rr = ‘<font color=#008000>$1$2$3</font>’ + ‘<font color=#008284>$4</font>’ + ‘<font color=#0000FF>$5$6$7</font>’ + ‘<font color=#000080><b>$8</b></font>’; re = new RegExp(rs,"g"); str = str.replace(re, rr); pres[i].innerHTML = "<pre>" + str + "</pre>"; } //End Delphi //C/C++ if (pres[i].className == "cpp") { str = pres[i].innerHTML; r = new RegExp(‘<(?!hr).+( |>)’,'gi’); var arr = str.match(r); if(arr != null) for(var n=0; n<arr.length; n++) { an = arr[n]; an = arr[n].replace(‘<’,'<’); an = an.toLowerCase(); str = str.replace(arr[n], an); } r1 = "((?:http:)?//.*)"; r2 = "(/\\*[\\s\\S]*?\\*/)"; r3 = ‘(".*?")’; r4 = "(‘.*?’)"; r5 = "(#.*)"; r6 = "\\b(" + CppKeywords + ")\\b"; rs = r1 + ‘|’ + r2 + ‘|’ + r3 + ‘|’ + r4 + ‘|’ + r5 + ‘|’ + r6; rr = ‘<font color=#008000>$1$2</font>’ + ‘<font color=#0000FF>$3</font>’ + ‘<font color=#800080>$4</font>’ + ‘<font color=#008284>$5</font>’ + ‘<font color=#000080><b>$6</b></font>’; re = new RegExp(rs,"g"); str = str.replace(re, rr); pres[i].innerHTML = "<pre>" + str + "</pre>"; } //End C/C++ //JavaScript if (pres[i].className == "js") { str = pres[i].innerHTML; r = new RegExp(‘<(?!hr).+( |>)’,'gi’); var arr = str.match(r); if(arr != null) for(var n=0; n<arr.length; n++) { an = arr[n]; an = arr[n].replace(‘<’,'<’); an = an.toLowerCase(); str = str.replace(arr[n], an); } r1 = "((?:http:)?//.*)"; r2 = "(/\\*[\\s\\S]*?\\*/)"; r3 = ‘(".*?")’; r4 = "(‘.*?’)"; r5 = "(#.*)"; r6 = "\\b(" + JsKeywords + ")\\b"; rs = r1 + ‘|’ + r2 + ‘|’ + r3 + ‘|’ + r4 + ‘|’ + r5 + ‘|’ + r6; rr = ‘<font color=#008000>$1$2</font>’ + ‘<font color=#CC0066>$3</font>’ + ‘<font color=#CC0066>$4</font>’ + ‘<font color=#008284>$5</font>’ + ‘<font color=#000080><b>$6</b></font>’; re = new RegExp(rs,"g"); str = str.replace(re, rr); pres[i].innerHTML = "<pre>" + str + "</pre>"; } //End JavaScript //cs if (pres[i].className == "cs") { str = pres[i].innerHTML; r = new RegExp(‘</?(?!hr)\\w+( |>)’,'gi’); var arr = str.match(r); if(arr != null) for(var n=0; n<arr.length; n++) { an = arr[n]; an = arr[n].replace(‘<’,'<’); an = an.toLowerCase(); str = str.replace(arr[n], an); } r1 = "(#if DBG[\\s\\S]+?#endif)"; r2 = "(#[a-z ]*)"; r3 = "(/// *<[/\\w]+>)"; r4 = "(/\\*[\\s\\S]*?\\*/)"; r5 = "((?:http:)?//.*)"; r6 = ‘(@?".*?")’; r7 = "(‘.*?’)"; r8 = "\\b(" + CsKeywords + ")\\b"; r9 = "\\b(" + CsClass + ")\\b"; rs = r1 + ‘|’ + r2 + ‘|’ + r3 + ‘|’ + r4 + ‘|’ + r5 + ‘|’ + r6 + ‘|’ + r7 + ‘|’ + r8 + ‘|’ + r9; rr = ‘<font color=#808080>$1$2$3</font>’ + ‘<font color=#008000>$4$5</font>’ + ‘<font color=#A31515>$6$7</font>’ + ‘<font color=#0000FF>$8</font>’ + ‘<font color=#2B91AF>$9</font>’; re = new RegExp(rs,"g"); str = str.replace(re, rr); pres[i].innerHTML = "<pre>" + str + "</pre>"; } //End cs } //}
以上是js脚本内容。
使用方法:
首先在 HTML 的 <head> 和 </head> 之间加上: <script defer type="text/javascript" src="上面的js.js"></script>
然后:
<pre class=Delphi>Delphi 代码</pre> <pre class=cs>C# 代码</pre> <pre class=cpp>C 或 C++ 代码</pre>