JavaScript-implemented CSS code highlighting _javascript tips

Source: Internet
Author: User
The syntax is much simpler than javascript,css, mainly dealing with annotations, strings, CSS style names, CSS style values, indenting, and wrapping, see code for details.
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <ptml xmlns=" http://www.w3.org/1999/xhtml "> <pead> <meta http-equiv=" Content-type "content=" text/html; charset=gb2312 "/> <title>javascript implementation CSS code highlighting </title> <style type=" Text/css "> body{font-s ize:12px; line-height:1.8; font-family: ' Courier New ', Courier, monospace; } #area {width:320px; height:120px; } </style> </pead> <body> <textarea id= "area" > body{font-size:12px; line-height:1.8; } #area {width:320px; line-height:1.5; font-family: "Courier New", Courier, monospace; }/* ul{margin:0; padding:0; } */</textarea> <button id= "btn" >this ' S testing......</button> <div ' pre ' ></div> id= Cript type= "Text/javascript" > function $ (ID) {return document.getElementById (ID); }; $ ("btn"). OncLick=function () {var code=$ ("area"). Value; Processing Note: Note Replaced by _ number _ var Startidx=endindex=-1; var at=0; var commentlist=[]; while (true) {Startindex=code.indexof ("/*", at); if (startindex==-1) break; Endindex=code.indexof ("* *", startIndex); if (endindex==-1) break; at=endindex+2; Commentlist.push (code.substring (Startindex,at)); Code=code.replace (Commentlist[commentlist.length-1], "_" + (Commentlist.length-1) + "_"); }//String Code=code.replace (/([' "]). *\1/g,function (m) {return" <span style=\ "color: #060; \" > +m+ "</sp An> "}); CSS style value code=code.replace (/:(. +);/g,function (m,n) {return ": <span style=\" color: #00F; \ ">" +n+ "</span>;"}) ; CSS style name Code=code.replace (/[{}]/g,function (m) {if (m== "{") {return "{<span style=\" color: #0 06;\ ">"; }else{return "&LT;/SPAN&GT;}"; } }); Comments Code=code.replace (/_ (\d+) _/g,function (m,n) {return "<span style=\" color: #999; \ "> +commentlist[n]+" </span > "}); Process \ t Code=code.replace (/^ (\t+)/gm,function (m) {return (new Array (m.length+1)). Join ("   &nbs p; ");}; Process Space Code=code.replace (/^ (+)/gm, function (m) {return (new Array (M.length + 1)). Join (" "); }); Handle Line Code=code.replace (/\r?\n/g, "<br>"); $ ("Pre"). Innerhtml=code; } </script> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

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.