1. 코드 복사 코드는 다음과 같습니다. <-- - 출처 : http://www.iecn.net/bbs/view/105499 .html< /a> - IECN.NET 2006-09-22 --> <br>type="text/ javascript"> ; <BR>//모든 주석 제거 <BR>String.prototype.DeleteComment = function () <BR>{ <BR> var str = this.replace(/(['"])(. ?)( [' "])/g,function(s,s1,s2,s3){return s1 s2.replace(/[/*]/g,"\$&") s3}); <BR> str = str. 교체( ///[^r] |/*[sS] ?*//g,"") <BR> str = str.replace(/(['"])(. ?)(['"] )/ g,function(s,s1,s2,s3){return s1 s2.replace(/\([^\])/g,"$1") s3}) <BR> return str; <BR>//형식 코드<BR>String.prototype.FormatCode = function () <BR>{ <BR> return this.replace(/\$/mg,"").replace(/[^s>;] $/ mg,"$&;"); <br>} <br>//문자열 앞과 뒤의 추가 공백을 삭제합니다. <br>String.prototype.Trim = function (m) <br>{ <br> return this .replace( m ? /^s*|s*$/mg : /^s*|s*$/g, "") <br>} <br>//코드 실행<br>function RunCode(obj) <br> { <br> window.open('','_blank').document.write(obj.value) <br>} <br><br> 테스트 코드: < ;br/> 🎜> 테스트 코드<br>*/ <br>var x=5; <br>document.write("x = // " x) <br>document.write("bh/* " x); >document.write("x =jjhj */hhhhhh " x); <br>document.write("x = // " x) <br><br>alert(22) <br><br>alert( 22) <br><br>var a="/fwe/////wefawef"; <br>alert(/^/*[^/]*$/.test(a)) <br>// 댓글<br> 1단계: 댓글 삭제 2단계: 코드 형식 지정 3단계: 양쪽 공백 제거 4단계: <버튼 onclick="RunCode(Text);">코드 실행 2. 코드 복사 코드는 다음과 같습니다. 형식 <br><!-- <BR>/**//**//** <BR>** ======================================== == =============================================== == ======= <BR>** 클래스 이름: CLASS_FORMATER <BR>** 함수: JS 형식화 <BR>** 예: <BR> ------------- --- ---------------------------------- --- ---------------------- <br><br> var xx = new CLASS_FORMATER( 코드); <br><br> document.getElementById("display").innerHTML = xx.format(); <br><br> ------------------ -------- ---------------- -------- --------------- <BR>** 작성자: ttyp <BR>** 이메일 : <a href="mailto :ttyp@21cn.com">ttyp@21cn.com</a> <br>** 날짜: 2006-5-21 <br>** 버전: 0.1 <br>* * ======= ========================================= ======== ========================================<br>*** <br>** =================== ================================================= ============================ <br>** 특정명:CLASS_FORMATER <br>** 功能:JS格式化 <br>** 예문: <br> ----------------------------- ------------------------------------- ------- <br><br> var xx = new CLASS_FORMATER(코드); <br><br> document.getElementById("display").innerHTML = xx.format(); <br><br> ------------------------------- ------------------------------------- ----- <br>** 작곡자:ttyp <br>** 邮件:<a href="mailto:ttyp@21cn.com">ttyp@21cn.com</a> <br>** 日期:2006-5-21 <br>** 版本:0.1 <br>** ======================== ================================================= ====================== <br>**/ <br><br>함수 CLASS_FORMAT(코드){ //哈希表类 함수 Hashtable(){ this._hash = new Object(); this.add = 함수(키, 값){ if(typeof(key)!="미정의"){ if(this.contain s(key)==false){ 이 ._hash[키]=typeof(값)=="정의되지 않음"?null:값; true를 반환합니다. } else { false를 반환합니다. } } 그렇지 않으면 { return false; } } this.remove = function(key){delete this._hash[key];} this.count = function(){var i=0;for(var k in this._hash){i ;} return i;}<br> this.items = function(key){return this._hash[key];} <br> this.contains = function(key){return typeof(this._hash[key])!="정의되지 않음";} <br> this.clear = function(){for(var k in this._hash){delete this._hash[k];}} <br><br> } <br><br> this._caseSensitive = true; <br><br> //字符串转换为哈希表 <br> this.str2hashtable = function(key,cs){ <br><br> var _key = key.split(/,/g); <br> var _hash = new Hashtable(); <br> var _cs = true; <br><br> <br> if(typeof(cs)=="undefed"||cs==null){ <br> _cs = this._caseSensitive; <br> } else { <br> _cs = cs; <br> } <br><br> for(var i in _key){ <br> if(_cs){ <br> _hash.add(_key[i]); <br> } else { <br> _hash.add((_key[i] "").toLowerCase()); <br> } <br><br> } <br> return _hash; <br> } <br><br> //获得需要转换的代码 <br> this._codetxt = code; <br><br> if(typeof(syntax)=="정의되지 않음"){ <br> 구문 = ""; <br> } <br><br> this._deleteComment = false; <br> //是否大写敏感 <br> this._caseSensitive = true; <br> //可以后face加块语句的关键字 <br> this._blockElement = this.str2hashtable("switch,if,while,try,finally"); <br> //是函数申明 <br> this._function = this.str2hashtable("function"); <br> //本行括号内分号不做换行 <br> this._isFor = "for"; <br><br> this._choiceElement = this.str2hashtable("else,catch"); <br><br> this._beginBlock = "{"; <br> this._endBlock = "}"; <br><br> this._singleEyeElement = this.str2hashtable("var,new,return,else,delete,in,case"); <br> //得到分割字符 <br> this._wordDelimiters= " ,.?!;:\/<>(){}[]"'rnt= -|*%@#$^&"; <br> //인용 문자 <br> this._quotation = this.str2hashtable("",'"); <br> //줄 주석 문자 <br> this._lineComment = "//"; <br> //이스케이프 문자<br> this._escape = "\" <br> //여러줄 인용 시작<br> this._commentOn = "/* “; _in 🎜><br><br> this.format = function() { <br> var codeArr = new Array() <br> var word_index = 0 <br> var htmlTxt = new Array(); <br><br> if(this.isCompress ){ <br> this._deleteComment = true; <br> } <br><br><br> //분할 문자 배열 가져오기(단어 분할) <br> for (var i = 0; i < this._codetxt.length; i ) { ~ || typeof(codeArr[word_index]) == '정의되지 않음') { <br> codeArr[word_index] = """ codeArr[ word_index] = this._codet xt.charAt(i); <BR> codeArr[word_index] = this._codetxt.charAt (i); <br><br><BR> var quote_opened = false; //인용 태그<BR> var slash_star_comment_opened = false; //여러 줄 주석 태그<BR> var slash_ slash_comment_opened = false; > var line_num = 1; 🎜> var Bracket_open = false; <BR> var for_open = false <<> </> // 분할을 누르고 <BR> 표시 for (var I = 0; i & lt; = word_index ; <br> if(typeof(codeArr[i])=="정의되지 않음"||codeArr[i].length==0){ | htmlTxt[htmlTxt .length] = codeArr[i]; <br> 도착[i]; "){ <br> else if(codeArr[i]=="r"){ slash_slash_comment_opened = 거짓; <BR> quote_opened = false; <BR> line_num ; <BR> if(!this.isCompress){ <BR> htmlTxt[htmlTxt.length] = "rn" this.getIdent(); <BR> } <BR> //처리 기능의 다양화 <BR> } 그렇지 않으면 (!slash_slash_comment_opened&&!slash_star_comment_opened && !quote_opened && this.isFunction(codeArr[i])){ <BR> htmlTxt[htmlTxt.length] = codeArr[i] " "; <BR> function_opened = true; <BR> } else if (!slash_slash_comment_opened&&!slash_star_comment_opened && !quote_opened &&codeArr[i]==this._isFor){ <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> for_open = true; <BR> } else if (!slash_slash_comment_opened&&!slash_star_comment_opened && !quote_opened &&codeArr[i]=="("){ <BR> bracket_open = true; htmlTxt[htmlTxt.length] = codeArr[i] <BR> } else if (!slash_slash_comment_opened&&!slash_star_comment_opened && !quote_opened &&codeArr[i]==")"){ <BR> bracket_open = false; <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> } else if (!slash_slash_comment_opened&&!slash_star_comment_opened && !quote_opened &&codeArr[i]==this._rowEnd){ <BR> if(!this.isCompress){ if(!for_open){ <BR> if( i<word_index&&(codeArr[i 1]!="r"&&codeArr[i 1]!="n")){ <BR> htmlTxt[htmlTxt.length] = codeArr[i] "n" this.getIdent(); <BR> }else{ <BR> htmlTxt[htmlTxt.length] = codeArr[i] this.getIdent(); <BR> } <BR> }else{ <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> } <BR> }else{ <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> } <BR> } else if(!slash_slash_comment_opened&&!slash_star_comment_opened && !quote_opened &&codeArr[i]==this._beginBlock){ <BR> _open = false; <BR> if(!this.isCompress){ <BR> 스위치(this.style){ <BR> 케이스 0 <BR> this._tabNum ; <BR> htmlTxt[htmlTxt.length] = codeArr[i] "n" this.getIdent(); <BR> 휴식; <BR> 사례 1: <BR> htmlTxt[htmlTxt.length] = "n" this.getIdent(); <BR> this._tabNum ; <BR> htmlTxt[htmlTxt.length] = codeArr[i] "n" this.getIdent(); <BR> 휴식; <BR> 기본값: <BR> this._tabNum ; <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> 휴식; <br><br> } <BR> }else{ <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> } <br><br> } else if(!slash_slash_comment_opened&&!slash_star_comment_opened && !quote_opened &&codeArr[i]==this._endBlock){ <BR> if(!this.isCompress){ <BR> this._tabNum-- ; <BR> if(i<word_index&&codeArr[i 1]!=this._rowEnd){ <BR> htmlTxt[htmlTxt.length] = "n" this.getIdent() codeArr[i]; <BR> }else{ <BR> htmlTxt[htmlTxt.length] = "n" this.getIdent() codeArr[i]; <BR> } <BR> }else{ <BR> if(i<word_index&&codeArr[i 1]!=this._rowEnd){ <BR> htmlTxt[htmlTxt.length] = codeArr[i] this._rowEnd; <BR> }else{ <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> } <BR> } <BR> //处理关键字 <BR> } 그렇지 않으면 (!slash_slash_comment_opened&&!slash_star_ comment_opened && !quote_opened && this.isBlockElement(codeArr[i])){ <BR> htmlTxt[htmlTxt.length ] = codeArr[i]; <BR> //处理内置对象(后面加一个空格) <BR> } else if (!slash_slash_comment_opened&&!slash_star_comment_opened && !quote_opened && this.isSingleE yeElement(codeArr[i])){ <BR> if(codeArr[i]= =this._in){ <BR> htmlTxt[htmlTxt.length] = " "; <BR> } <BR> htmlTxt[htmlTxt.length] = codeArr[i] " "; <BR> //处理双引号(引号前不能为转义字符) <BR> } else if (!slash_star_comment_opened&&!slash_slash_comment_opened&&this._quotation.contains(codeArr[i])){ <BR> if (quote_opened){ <BR> //是상应의 引号 <BR> if(quote_char==codeArr[i]){ <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> quote_opened = false; <BR> quote_char = ""; <BR> } else { <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> } <BR> } else { <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> quote_opened = true; <BR> quote_char = codeArr[i]; <BR> } <BR> //处理转义字符 <BR> } else if(codeArr[i] == this._escape){ <BR> htmlTxt[htmlTxt.length] = codeArr[i]; <BR> if(i<word_index-1){ <BR> if(codeArr[i 1].charCodeAt(0)>=32&&codeArr[i 1].charCodeAt(0)<=127){ <BR> htmlTxt [htmlTxt.length] = codeArr[i 1].substr(0,1); <BR> htmlTxt[htmlTxt.length] = codeArr[i 1].substr(1); <BR> i=i 1; <BR> } <BR> } <BR> /////处理多行注释的开始 <BR> } else if (!slash_slash_comment_opened && !slash_star_comment_opened&&!quote_opened&&this.isStartWith(this._commentOn,codeArr,i)){ slash_star_comment_opened = true; <BR> if(!this._deleteComment){ <BR> htmlTxt[htmlTxt.length] = this._commentOn; <BR> } <BR> i = i this.getSkipLength(this._commentOn); <BR> //处理单行注释 <BR> } else if (!slash_slash_comment_opened && !slash_star_comment_opened&&!quote_opened&&this.isStartWith(this._lineComment,codeArr,i)){ <BR> slash_slash_comment_opened = true; <BR> if(!this._deleteComment){ <BR> htmlTxt[htmlTxt.length] = this._lineComment; <BR> } <BR> i = i this.getSkipLength(this._lineComment); <BR> //处理忽略词 <BR> <BR> slash_slash_comment_opened = true; <BR> htmlTxt[htmlTxt.length] = this._ignore; <BR> i = i this.getSkipLength(this._ignore); <BR> //처리다행注释结束 <BR> } else if (!quote_opened&&!slash_slash_comment_opened&&this.isStartWith(this._commentOff,codeArr,i)){ <BR> if (slash_star_comment_opened) { <BR> slash_star_comment_opened = false; <BR> if(!this._deleteComment){ <BR> htmlTxt[htmlTxt.length] = this._commentOff; <BR> }<BR> //문자열에 없음 <BR> ~ 댓글에는 없음 <BR> if(!slash_slash_comment_opened && !slash _star_comment_opened){ if(! <BR> 🎜> }그밖에{ <BR> ~ 와 함께 = function(str,code,index ){ <BR><BR> if(typeof(str)!="undefine"&&str.length>0){ var cc = new Array() for(var i=index;i< ;색인 str.length;i ){ <br> cc[cc.length] <br> if(str.length>=code[index].length&&c.indexOf(str)==0){ <br> if(str .length> ;=code[index].length&&c.toLowerCase().indexOf(str.toLowerCase())==0){ <br> <br> false를 반환합니다. <br><br> } else { <br> false를 반환합니다. <br> } <br> } <br><br> this.isFunction = function(val){ <br> return this._function.contains(this._caseSensitive?val:val.toLowerCase()); <br> } <br><br> this.isBlockElement = function(val) { <br> return this._blockElement.contains(this._caseSensitive?val:val.toLowerCase()); <br> } <br><br> this.isChoiceElement = function(val) { <br> return this._choiceElement.contains(this._caseSensitive?val:val.toLowerCase()); <br> } <br><br> this.isSingleEyeElement = function(val) { <br> return this._singleEyeElement.contains(this._caseSensitive?val:val.toLowerCase()); <br> } <br><br> this.isNextElement = function(from,word){ <br> for(var i=from;i<word.length;i ){ <BR> if(word[i]! =" "&&word[i]!="t"&&word[i]!="r"&&word[i]!="n"){ <BR> return this.isChoiceElement(word[i]); <BR> } <BR> } <BR> false를 반환; <BR> } <br><br> this.getSkipLength = function(val){ <BR> var count = 0; <BR> for(var i=0;i<val.length;i ){ <BR> if(this._wordDelimiters.indexOf(val.charAt(i))>=0){ <br> 개수 ; <br> } <br> } <br> if(count>0){ <br> count=count-1; <br> } <br> 반품 횟수; <br> } <br><br> this.getIdent=function(){ <br> var n = []; <br> for(var i=0;i<this._tabNum;i ){ <BR> n[n.length] = "t"; <BR> } <BR> return n.join(""); <BR> } <BR>} <br><br>함수 doformat(o){ <BR> var htmltxt = ""; <br><br> if (o == null){ <BR> alert("domNode가 null입니다!"); <BR> 반품; <BR> }<br><br> var _codetxt = ""; <br><br> if(typeof(o)=="object"){ <BR> switch(o.tagName){ <BR> case "TEXTAREA": <BR> case "INPUT" <BR> _code txt = o.값; <BR> 휴식; <BR> 케이스 'DIV': <BR> 케이스 'SPAN': <BR> _codetxt = o.innerText; <BR> 휴식; <BR> 기본값: <BR> _codetxt = o.innerHTML; <BR> 휴식; <BR> } <BR> }else{ <BR> _codetxt = o; <BR> } <br><br> var _syn = new CLASS_FORMAT(_codetxt); <BR> htmltxt = _syn.format(); <BR> return htmltxt; <BR>} <br><br><BR>function go() <BR>{ <BR> var code = document.getElementById("code").value; <BR> var xx = 새로운 CLASS_FORMAT(코드); <BR> var a = new Date(); <br><br> if(document.getElementById('cboOperate').selectedIndex==1){ <BR> xx.isCompress=true; <BR> }else{ <BR> xx.style = parseInt(document.getElementById('cboStyle').value); <BR> } <BR> document.getElementById("display").value = xx.format(); <BR> alert("共꽃:" (new Date().getTime()-a.getTime()) "ms"); <BR>} <BR>//--> <br> /* 这个是一个类 */ 함수 xx (num,str){//说明 var a = num;this.aa = a ; this.bb = function(){alert(str);} this.cc = function(){for(var i=0;i<10;i ){document.title=i;}} }; xx.prototype.dd= function(){alert("d d kd");} var a = new xx(100,"hello"),b=new xx( 0,"typ"); if(1>2) { alert(); } else { alert(" 젠장 "); } a.bb(); b.dd(); 경고(a.aa); 格式化 压缩 经典 微软