简单JS代码压缩器
2024-09-06 12:44:48
供稿:网友
1、
代码如下:
<!--
- From: <a href="http://www.iecn.net/bbs/view/105499.html" target="_blank">http://www.iecn.net/bbs/view/105499.html</a>
- IECN.NET 2006-09-22
-->
<script language="javascript"
type="text/javascript">
//去除所有的注释
String.prototype.DeleteComment = function ()
{
var str = this.replace(/(['"])(.+?)(['"])/g,function(s,s1,s2,s3){return s1+s2.replace(/[///*]/g,"//$&")+s3});
str = str.replace(/////[^/r]+|///*[/s/S]+?/*///g,"");
str = str.replace(/(['"])(.+?)(['"])/g,function(s,s1,s2,s3){return s1+s2.replace(///([^//])/g,"$1")+s3});
return str;
}
//格式代码
String.prototype.FormatCode = function ()
{
return this.replace(///$/mg,"").replace(/[^/s>;]$/mg,"$&;");
}
//删除字符串前后多余的空格
String.prototype.Trim = function (m)
{
return this.replace(m ? /^/s*|/s*$/mg : /^/s*|/s*$/g, "");
}
//运行代码
function RunCode(obj)
{
window.open('','_blank').document.write(obj.value);
}
</script>
测试代码:<br/>
<textArea id="Text" rows="25" cols="80">
<script language="JavaScript">
/*
测试代码
*/
var x=5;
document.write("x = // " + x);
document.write("bh/* " + x);
document.write("x =jjhj */hhhhhh " + x);
document.write("x = // " + x);
alert(22)
alert(22)
var a="/fwe/////wefawef";
alert(/^//*[^//]*$/.test(a));
//注释
</script>
</textArea> <br/><br/>
第1步:<button onclick="Text.value = Text.value.DeleteComment();">删除注释</button> <br/> <br/>
第2步:<button onclick="Text.value=Text.value.FormatCode();">格式代码</button> <br/> <br/>
第3步:<button onclick="Text.value=Text.value.Trim(true);">去除两边空格</button> <br/> <br/>
第4步:<button onclick="RunCode(Text);">运行代码</button> <br/> <br/>
2、
代码如下:<HTML><HEAD><TITLE>Format</TITLE>
<META content="MSHTML 6.00.2800.1528" name=GENERATOR>
<META content="" name=Author>
<META content="" name=Keywords>
<META content="" name=Description></HEAD>
<BODY>
<SCRIPT language=JavaScript>