Removing comments in JavaScript using Ruby

2019-08-01 08:00发布

I need a regex or something to remove this kind of comments.

/*!
 * Foo Bar
 */

I tried with /(\/*!.**\/)/m but fails. Any Suggestion?

7条回答
唯我独甜
2楼-- · 2019-08-01 08:23

Simple regexps for removing (c-style) single-line and multi-line comments. But note that those are really simple - for example they don't take possibility of /* */ inside a string literal. But should be fine for removing comments from a simple code:

str.gsub!(/\/\*.*?\*\//m, '') # remove multiline comments
str.gsub!(/\s*\/\/.*/, '') # remove single-line comments
查看更多
Bombasti
3楼-- · 2019-08-01 08:28

I would highly suggest removing these comment with Google's Closure Compiler, I have used it in production and it's fantastic.

查看更多
聊天终结者
4楼-- · 2019-08-01 08:29

Well i think i got what you want :)

<html>
<head>
<title>Code Condenser</title>
<style type="text/css">
#source{font-size:10pt;width:90%;height:300px;}
body{text-align:center;}
</style>
<script type="text/javascript">
function vc_js_condenser(){var a=/[a-zA-Z0-9._'"]/;var b=/[a-zA-Z0-9._]/;var c=/[a-zA-Z0-9._\]\)"']/;
function d(e,f){e=e.replace(/[\n\r]+/g,"\n");var g=(typeof(f)=="boolean")?f:false;var h=["var","else",
"new","do","case","with","typeof","delete","try","finally","throw","in","instanceof"];var i=0;var j=
new Array();var k=e.split('');l=k.length;var m=new Array();var G=null;var o=false;var p=[["'","'","\\"]
,['"','"','\\'],["//","\n",null],["/*","*/",null]];var q="[](){},;/\n";var r;function s(){if(m.length<1)
return;var u=m.join('');if((!g)||((u.match(/^\/\//)==null)&&(u.match(/^\/\*/)==null))){if(!((u=="\n")&&
((G==';')||(G=="\n")))){if((G!=null)&&(G.match(a)!=null)&&(u.match(a)!=null))j[j.length]=' ';j[j.length]
=u;G=u;}}m.length=0;v=false;}var v=false;var w=false;while(i<l){if(!o){if((k[i]==" ")||(k[i]=="\t")){s();
}else{var x=false;if(q.indexOf(k[i])>-1){if(k[i]!='/'){x=true;s();}else if((m.length>0)&&(m[0]!='/')){
s();}}for(var y=0;y<p.length;y++){var z='';if((m.length>0)&&(m[m.length-1]==p[y][0].charAt(0)))z=m[
m.length-1];var A=z+k[i];if(A==p[y][0]){o=true;r=y;if(z!='')m.length--;if(m.length>0)s();if(z!='')m
[m.length]=z;break;}}if((k[i]=="/")&&(!o)&&(m.length==0)&&(G!=null)&&(G.match(c)==null)&&(i+1<k.length)
&&(k[i+1]!='/')){o=true;r=null;}var B=k[i].match(b)!=null;if(m.length<1)v=B;if(v!=B){s();v=B;}m[m.length]
=k[i];if(x)s();}}else{m[m.length]=k[i];var C=m.join('');if(r!=null){var D=C.lastIndexOf(p[r][1]);if((D!=-1)
&&(D>0)){if(m[D-1]==p[r][2]){if((D>2)&&(m[D-2]==p[r][2])){s();o=false;}}else{s();o=false;}}}else{if((w==false)
&&(k[i]=="/")){i++;while((i<k.length)&&(k[i].match(b)!=null)){m[m.length]=k[i];i++;}s();o=false;i--;}else{if
(!w&&(k[i]=="\\"))w=true;else w=false;}}}i++;}s();var E=-1;for(var i=0;i<j.length;i++){var u=j[i];var
F=(i>0)?j[i-1]:null;var n=(i<(j.length-1))?j[i+1]:null;if(u=="\n"){if((F!=null)&&(n!=null)){if((F.match(c)
!=null)&&(n.match(a)!=null)){u=';';for(var H=h.length-1;H>-1;H--){if(F==h[H])u=' ';}j[i]=u;E++;if((E<j.length)
&&(E>-1)&&(i!=E))j[E]=j[i];}}}else{E++;if((E<j.length)&&(E>-1)&&(i!=E))j[E]=j[i];}}j.length=E+1;return j;}var
I=document.getElementById('source');var J=I.value.length;I.value=d(I.value,document.getElementById('uncomment').checked
==true).join('');var K=I.value.length;var F=document.getElementById("saved");F.removeChild(F.firstChild);var
L=document.createElement("span");F.appendChild(L);L.appendChild(document.createTextNode("Original Size: "+
J+" characters"));L.appendChild(document.createElement("br"));L.appendChild(document.createTextNode(
"Condensed Size: "+K+" characters"));L.appendChild(document.createElement("br"));L.appendChild(
document.createTextNode("Size Difference: "+(J-K)+" characters"));if(J!=0){L.appendChild(document.createElement
("br"));L.appendChild(document.createTextNode("Condensed Code is "+(Math.floor(K/J*10000)/100)+
"% of original size."));}}
</script>
</head>
<body>
<h1>JavaScript Source Code Condenser</h1>
<p>Paste only valid Javascript (no HTML tags) in the box below.</p>
<button onclick="vc_js_condenser();">Condense Code</button>&nbsp;<input type="checkbox" checked="checked" id="uncomment" />Remove Comments&nbsp;<button onclick="document.getElementById('source').select();">Select All</button><br />
<textarea id="source"></textarea><br />
<p id="saved"><span></span></p>
</body>
</html>

Kindly save this script as HTML and then run it in your browser then copy your javascript and put it in the texterea and then condense it with the button

This code will remove the spaces and comments on your code so please dont forget end your functions with ; to avoid syntax error.

This will help you reducing your javascript files size this will put ur javascript in 1 line , so if you need it back as well formed u can use this addon in firefox if u use it http://www.formatjavascript.com/

Regards :) please mark this answer as the correct one if u see it useful

查看更多
做自己的国王
5楼-- · 2019-08-01 08:30

You need to escape the * too:

/(\/\*!.*\*\/)/m

check this out: http://www.rubular.com/r/n2bV9qUD6k

查看更多
手持菜刀,她持情操
6楼-- · 2019-08-01 08:37

You could use a javascript compressor like YUI compress or Google Closure Compiler

查看更多
孤傲高冷的网名
7楼-- · 2019-08-01 08:40

* has to be escaped, otherwise it matches zero or more occurences of / (in case of \/*). There is an syntax error as well in .**. Try the next RE with * properly escaped:

/\/\*!.*?\*\//m
查看更多
登录 后发表回答