1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | 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]*?\\*\\))" ; // (* *) 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脚本内容。
使用方法:
1 | 首先在 HTML 的 < head > 和 </ head > 之间加上: < script defer type = "text/javascript" src = "上面的js.js" ></ script > |
然后:
1 2 3 | < pre class = Delphi >Delphi 代码</ pre > < pre class = cs >C# 代码</ pre > < pre class = cpp >C 或 C++ 代码</ pre > |