Removing comments in JavaScript using Ruby - Stack Overflow

I need a regex or something to remove this kind of ments.*!* Foo Bar*I tried with (*!.**)m but

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

/*!
 * Foo Bar
 */

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

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

/*!
 * Foo Bar
 */

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

Share Improve this question edited Apr 2, 2011 at 11:30 Bart Kiers 170k37 gold badges306 silver badges295 bronze badges asked Apr 2, 2011 at 11:01 DAddYEDAddYE 1,72911 silver badges16 bronze badges 4
  • Given the plexity of JavaScript syntax (which while fairly simple by programming language standards is still plex), the odds of your successfully doing this reliably with a regex seem remote. Suggest not re-inventing the wheel and instead using something that's already been written, such s shelling out to jsmin or similar. – T.J. Crowder Commented Apr 2, 2011 at 11:04
  • Why recreate the wheel when there are libraries that do it already? Plus what about var re = /funk\/*/; will your code be smart enough to ignore it? :) – epascarello Commented Apr 2, 2011 at 12:39
  • You need to be very careful because this regex can match ment delimiters which are not really ment delimiters. In other words, /* or */ or // can appear within a string literal in which case you do NOT want to remove them. e.g. "This string has /* in it and should not be touched!" – ridgerunner Commented Apr 2, 2011 at 15:43
  • Upon closer inspection, I see that you are looking for a specific type of C-ment. Please disregard my previous ment (and see my suggested answer). – ridgerunner Commented Apr 2, 2011 at 16:37
Add a ment  | 

7 Answers 7

Reset to default 3

To do it accurately and efficiently, there is a better regex:

regexp = /\/\*![^*]*\*+(?:[^*\/][^*]*\*+)*\//
result = subject.gsub(regexp, '')

Jeffrey Friedl covers this specific problem at length (using C-ments as an example) in his classic work: Mastering Regular Expressions (3rd Edition). Here is a breakdown of the regex which illustrates the "Unrolling-the-Loop" efficiency technique.

regexp_long = / # Match she-bang style C-ment
    \/\*!       # Opening delimiter.
    [^*]*\*+    # {normal*} Zero or more non-*, one or more *
    (?:         # Begin {(special normal*)*} construct.
      [^*\/]    # {special} a non-*, non-\/ following star.
      [^*]*\*+  # More {normal*}
    )*          # Finish "Unrolling-the-Loop"
    \/          # Closing delimiter.
    /x
result = subject.gsub(regexp_long, '')

Note that this regex does not need Ruby's 'm' dot-matches-all modifier because it does not use the dot!

Additional: So how much more efficient is this regex over the simpler /\/\*!.*?\*\//m expression? Well using the RegexBuddy debugger, I measured how many steps each regex took to match a ment. Here are the results for both matching and non-matching: (For the non-,matching case I simply removed the last / from the ment)

/*!
 * This is the example ment
 * Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar
 * Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar
 * Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar
 */

'
REGEX                        STEPS TO: MATCH  NON-MATCH
/\/\*!.*?\*\//m                        488      491
/\/\*![^*]*\*+(?:[^*\/][^*]*\*+)*\//    23       29
'

As you can see, the lazy-dot solution (which must backtrack once for each and every character in the ment), is much less efficent. Note also that the efficiency difference is even more pronounced with longer and longer ments.

CAVEAT Note that this regex will fail if the opening delimiter occurs inside a literal string, e.g. "This string has a /*! in it!". To do this correctly with 100% accuracy, you will need fo fully parse the script.

* 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

You need to escape the * too:

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

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

Simple regexps for removing (c-style) single-line and multi-line ments. 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 ments from a simple code:

str.gsub!(/\/\*.*?\*\//m, '') # remove multiline ments
str.gsub!(/\s*\/\/.*/, '') # remove single-line ments

You could use a javascript pressor like YUI press or Google Closure Compiler

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('unment').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="unment" />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 ments 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./

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745199982a4616278.html

相关推荐

  • Removing comments in JavaScript using Ruby - Stack Overflow

    I need a regex or something to remove this kind of ments.*!* Foo Bar*I tried with (*!.**)m but

    5小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信