Javascript highlights the code of the program code

Source: Internet
Author: User
Tip: you can modify some code before running

Javascript highlights the code of the program code<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Jslib testing</title><style type="text/css">Pre {color: black;} span. hl_js_kw {color: blue; font-weight: bold;} span. hl_js_mcm {color: green;} span. hl_js_scm {color: gray;} span. hl_js_s {color: green;} span. hl_js_reg {color: red;} span. hl_js_num {color: red;} span. hl_js_typ {color: # 9900CC ;}</style><script type="text/javascript">Function $ (id) {return document. getElementById (id);} function btn_click () {var formats = []; formats [JSEngine. TYPE_KEYWORD] = {"start": "", "end": ""}; formats [JSEngine. TYPE_MUTILCOMMENT] = {"start": "", "end": ""}; formats [JSEngine. TYPE_SINGLECOMMENT] = {"start": "", "end": ""}; formats [JSEngine. TYPE_STRING] = {"start": "", "end": ""}; formats [JSEngine. TYPE_REGEXP] = {"start": "", "end": ""}; formats [JSEngine. TYPE_NUMBER] = {"start": "", "end": ""}; formats [JSEngine. TYPE_TYPES] = {"start": "", "end": ""}; var pre = $ ("codePre"); var area = $ ("codeArea "); var engine = new JSEngine (); var code = area. value. replace (/</g, "<").replace(/>/G, ">"); var dt = new Date (); str = engine. parseSyntax (code, formats); $ ("msg "). innerHTML = "total" + code. length + "characters, parsing flowers" + (new Date (). getTime ()-dt. getTime () + "ms"; str = str. replace (/^ (t +)/gm, function (m) {return (new Array (m. length + 1 )). join ("") ;}); str = str. replace (/^ (+)/gm, function (m) {return (new Array (m. length + 1 )). join ("") ;}); str = str. replace (/r? N/g ,"<br/>"); Pre. innerHTML = str;} window. onload = function () {$ ("btn "). onclick = btn_click ;} var keywords = "Array | arguments | Boolean | Date | Enumerator | Error | Function | Global | Math | Number | Object | RegExp | String | break | delete | function | return | typeof | case | do | if | switch | var | catch | else | in | this | void | continue | false | instanceof | throw | while | debugger | finally | new | true | with | default | for | null | try | abstract | double | goto | native | static | B Oolean | enum | implements | package | super | byte | export | import | private | synchronized | char | extends | int | protected | throws | class | final | interface | public | transient | const | float | long | short | volatile "; var types = "Anchor | Applet | Area | Arguments | Array | Boolean | Button | Checkbox | Collection | Crypto | Date | Dictionary | Document | Drive | Drives | Element | Enumerator | Event | file | FileObject | FileSystemObject | FileUpload | Folder | Folders | Form | Frame | Function | Global | Hidden | History | HTMLElement | Image | Infinity | Input | JavaArray | JavaClass | JavaObject | JavaPackage | JSObject | Layer | Link | Math | MimeType | Navigator | Number | object | Option | Packages | Password | Plugin | PrivilegeManager | Random | RegExp | Screen | Select | String | Submit | Text | Textarea | URL | VBArray | Window | WScript | window "; function JSEngine () {}; JSEngine. TYPE_KEYWORD = 0; JSEngine. TYPE_MUTILCOMMENT = 1; JSEngine. T YPE_SINGLECOMMENT = 2; JSEngine. TYPE_STRING = 3; JSEngine. TYPE_REGEXP = 4; JSEngine. TYPE_NUMBER = 5; JSEngine. TYPE_TYPES = 6; var _ p = JSEngine. prototype; var _ p = JSEngine. prototype; var _ p = JSEngine. prototype; _ p. parseSyntax = function (code, formats) {var buff = [], codeBuff = code, codeLen = code. length, sIdx = 0, buffIdx = 0, objBuff = []; var stopIdx = codeLen, searchResult = null, result = null, len = 0; var flgs = {"string1": true, "string2": true, "mutilcm": true, "singlecm": true, "regcm": true }; while (flgs ["string1"] | flgs ["string2"] | flgs ["mutilcm"] | flgs ["singlecm"] | flgs ["regcm"]) {searchResult = null; codeLen = code. length; stopIdx = codeLen; if (flgs ["string1"]) {result = searchString (code, codeLen, sIdx, stopIdx, "); switch (result) {case null: flgs ["string1"] = false; break; ca Se-1: break; default: {searchResult = result; searchResult [3] = JSEngine. TYPE_STRING; stopIdx = result [0]; break ;}}if (flgs ["string2"]) {result = searchString (code, codeLen, sIdx, stopIdx, "'"); switch (result) {case null: flgs ["string2"] = false; break; case-1: break; default: {searchResult = result; searchResult [3] = JSEngine. TYPE_STRING; stopIdx = result [0]; break ;}}if (flgs ["mutilcm"]) {result = sea RchMutilComments (code, codeLen, sIdx, stopIdx); switch (result) {case null: flgs ["mutilcm"] = false; break; case-1: break; default: {searchResult = result; searchResult [3] = JSEngine. TYPE_MUTILCOMMENT; stopIdx = result [0]; break ;}}if (flgs ["singlecm"]) {result = searchSingleComments (code, codeLen, sIdx, stopIdx ); switch (result) {case null: flgs ["singlecm"] = false; break; case-1: break; default: {search Result = result; searchResult [3] = JSEngine. TYPE_SINGLECOMMENT; stopIdx = result [0]; break ;}}if (flgs ["regcm"]) {result = searchRegexp (code, codeLen, sIdx, stopIdx ); switch (result) {case null: flgs ["regcm"] = false; break; case-1: break; default: {searchResult = result; searchResult [3] = JSEngine. TYPE_REGEXP; break ;}}if (searchResult = null) {buff. push (code); break;} else {/* len = searchResult [1]-se ArchResult [0] + searchResult [2]; if (searchResult [0]! = 0) {buff. push (code. substring (sIdx, searchResult [0])} buff. push ([code. substring (searchResult [0], searchResult [1] + searchResult [2]), searchResult [3]); sIdx = searchResult [1] + searchResult [2]; code = code. substr (sIdx); sIdx = 0; */len = searchResult [1]-searchResult [0] + searchResult [2]; if (searchResult [0]! = 0) {buff. push (code. substring (sIdx, searchResult [0])} objBuff. push ([code. substring (searchResult [0], searchResult [1] + searchResult [2]), searchResult [3]); buff. push ("<_" + (objBuff. length-1) + "_>"); sIdx = searchResult [1] + searchResult [2]; code = code. substr (sIdx); sIdx = 0 ;}// dt = new Date ();/* for (I = 0, len = buff. length; I <len; I ++) {if (typeof (buff [I]) = "string") {reg = new RegExp ("B (" + Keywords + ") B", "gm"); result = formats [JSEngine. TYPE_KEYWORD]. start + "$1" + formats [JSEngine. TYPE_KEYWORD]. end; buff [I] = buff [I]. replace (reg, result); reg = new RegExp ("B (" + types + ") B", "gm"); result = formats [JSEngine. TYPE_TYPES]. start + "$1" + formats [JSEngine. TYPE_TYPES]. end; buff [I] = buff [I]. replace (reg, result); reg =/B ([+-]? (? : D + (? :. D *)? (? : E [+-]? D + )? | 0x [dA-F] +) B/gim; result = formats [JSEngine. TYPE_NUMBER]. start + "$1" + formats [JSEngine. TYPE_NUMBER]. end; buff [I] = buff [I]. replace (reg, result);} else {buff [I] = ([formats [buff [I] [1]. start, buff [I] [0], formats [buff [I] [1]. end]). join ("") ;}} * // alert (new Date (). getTime ()-dt. getTime (); buff = buff. join (""); reg = new RegExp ("B (" + keywords + ") B", "g"); result = formats [JSEngine. TYPE_KEYWORD]. start + "$1" + formats [JSEngine. TYPE_KEYWORD]. end; buff = buff. replace (reg, result); reg = new RegExp ("B (" + types + ") B", "g"); result = formats [JSEngine. TYPE_TYPES]. start + "$1" + formats [JSEngine. TYPE_TYPES]. end; buff = buff. replace (reg, result); reg =/B ([+-]? (? : D + (? :. D *)? (? : E [+-]? D + )? | 0x [dA-F] +) B/gim; result = formats [JSEngine. TYPE_NUMBER]. start + "$1" + formats [JSEngine. TYPE_NUMBER]. end; buff = buff. replace (reg, result); buff = buff. replace (/<_ (d +) _>/g, function (m, n) {return formats [objBuff [n] [1]. start + objBuff [n] [0] + formats [objBuff [n] [1]. end ;}); // return buff. join (""); return buff;} function searchMutilComments (code, codeLen, sIdx, stopIdx) {var startIdx =-1, endIdx =-1; if (CodeLen <4) return null; startIdx = code. indexOf ("/*", sIdx); if (startIdx =-1) return null; if (startIdx> stopIdx) return-1; endIdx = code. indexOf ("*/", startIdx + 2); if (endIdx =-1) return null; return [startIdx, endIdx, 2];} function searchString (code, codeLen, sIdx, stopIdx, ch) {var at = sIdx, I = 0, isEscape = false, startIdx =-1, endIdx =-1, len = 0; var isMutilLine = false, fragments = Null, isContinueSearch = false, tCh = null, fragm = null, fragmLen = 0; if (codeLen <2) return null; while (true) {startIdx = code. indexOf (ch, at); if (startIdx =-1) return null; if (startIdx> stopIdx) return-1; if (startIdx> at) {I = startIdx, isEscape = false; while (code. charAt (-- I) = "") isEscape =! IsEscape; if (isEscape) {at = startIdx + 1; if (at + 2)> stopIdx) return-1; continue ;}} break;} if (startIdx> stopIdx) return-1; at = startIdx + 1; lab_searchString: while (true) {endIdx = code. indexOf (ch, at); if (endIdx =-1) return null; if (endIdx> at) {I = endIdx, isEscape = false; while (code. charAt (-- I) = "") isEscape =! IsEscape; if (isEscape) {at = endIdx + 1; continue ;}} break;} isMutilLine = code. indexOf ("n", startIdx + 1); if (isMutilLine! =-1 & isMutilLine <endIdx) {fragments = code. substring (startIdx + 1, endIdx ). split ("n"); len = fragments. length-1; for (I = 0; I <len; I ++) {fragm = fragments [I]; fragmLen = fragm. length; tCh = fragm. charCodeAt (fragmLen-1) = 13? Fragm. charAt (fragmLen-2): fragm. charAt (fragmLen-1); if (tCh! = "") {If (endIdx> stopIdx) return-1; if (endIdx + 1) = stopIdx) return-1; startIdx = endIdx; at = startIdx + 1; continue lab_searchString ;}} break;} return [startIdx, endIdx, 1];} function searchSingleComments (code, codeLen, sIdx, stopIdx) {var at = sIdx, I = 0, isEscape = false, startIdx =-1, endIdx =-1; if (codeLen <2) return null; while (true) {startIdx = code. indexOf ("//", at); if (startIdx =- 1) return null; if (startIdx> stopIdx) return-1; if (startIdx> at) {I = startIdx, isEscape = false; while (code. charAt (-- I) = "") isEscape =! IsEscape; if (isEscape) {at = startIdx + 2; if (at + 3)> stopIdx) return-1; continue ;}} break;} endIdx = code. indexOf ("n", startIdx + 2); return [startIdx, endIdx =-1? Code. length: endIdx, 0];} function searchRegexp (code, codeLen, sIdx, stopIdx) {var at = sIdx, I = 0, isEscape = false, tmpResult = null, ch = null, endAt = 0, tmpStr = null; var startIdx =-1, tSIdx =-1, eStopIdx =-1, endIdx =-1, escapeFlg = "", bIdx =-1, eIdx =-1, bAt =-1, eAt =-1; function sEnd (code, startIdx) {var endIdx =-1, endAt = startIdx, I = 0, isEscape = false; while (true) {endIdx = Code. indexOf ("/", endAt); if (endIdx =-1) return-1; I = endIdx, isEscape = false; while (code. charAt (-- I) = "") isEscape =! IsEscape; if (isEscape) {endAt = endIdx + 2; continue;} else {return endIdx ;}} if (codeLen <2) return null; while (true) {startIdx = code. indexOf ("/", at); if (startIdx =-1) return null; if (startIdx> stopIdx) return-1; switch (code. charAt (startIdx + 1) {case null: case "r": case "n": case "*": case "/": {at = startIdx + 2; if (at + 2)> stopIdx) return-1; continue ;}} if (startIdx> at) {I = startIdx, is Escape = false; while (code. charAt (-- I) = "") isEscape =! IsEscape; if (isEscape) {at = startIdx + 2; if (at + 3)> stopIdx) return-1; continue ;}} tmpResult = code. indexOf ("n", startIdx + 1); // different rows of eStopIdx = tmpResult =-1? CodeLen: tmpResult; tmpStr = code. substring (0, startIdx); if (! /(? :[! = (,: +-* % & | !~ [/N ;{}? Rn] | typeof | in | instanceof | void | new | return | do | else | throw | ^) [t] * $ /. test (tmpStr) {at = startIdx + 1; continue;} endAt = (code. charAt (startIdx + 1) = "["? 1: 2) + startIdx; endIdx = sEnd (code, endAt); if (endIdx =-1) return null; bAt = startIdx + 1; while (true) {bIdx = code. indexOf ("[", bAt); if (bIdx =-1) break; I = bIdx, isEscape = false; while (code. charAt (-- I) = "") isEscape =! IsEscape; if (isEscape) {bAt = bIdx + 1; continue;} if (bIdx> endIdx) break; eAt = bIdx + 2; while (true) {eIdx = code. indexOf ("]", eAt); if (eIdx =-1) break; I = eIdx, isEscape = false; while (code. charAt (-- I) = "") isEscape =! IsEscape; if (isEscape) {eAt = eIdx + 1; continue;} if (eIdx> endIdx) {endAt = eIdx + 1; endIdx = sEnd (code, endAt ); if (endIdx =-1) return null;} bAt = eIdx + 1; break;} if (endIdx> eStopIdx) {at + = 1; continue;} break ;} return [startIdx, endIdx, 1];}</script></head><body><p id="msg">Enter the code to test it. If it is PHP output, use htmlspecialchars to encode it.</p><textarea id="codeArea" >// Test 1 function myfun (id) {/* this is mutilcomments "string" keyword: function */var str = "this is a string, /* fasasdf * // fdsasdf/^ reg/"; var reg =/^ fdadfasdf $/ig; return" myvalue "; // hahah} // test 2if (str. match (/(? :(? :[! = (,:] | [| N) [t] */$) | ^ n? [T] */$/) {} // test 3a = 1b = 2g = {test: function () {return 1} c = a // *** // B/g. test (); alert (c); // test 4 function t4 (test) {return (test /*/*//'"{; */& // */"'{; test &&"/*//"'{;"&&'/*//"'{; '& test) ;}// test 5var rexT5 =/[/] [+] ([Ss] *?) (? : [+] [/] | $) | [/] [/] (. *) | "((? : "| [^"]) *) "| '((? : '| [^']) *) '/G; // regular var nu = 1/2/3 // This is the division var str = "123 // 456 ". replace (/[/]/g, '') + 1/2/3; // commentvar o = [/re/, (/re/), typeof/re /, function () {return/re/}, 'O' in/re/, 1 // re /,! /Re/];


Tip: you can modify some code before running

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.