1

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

/*!
 * Foo Bar
 */

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

4
  • Given the complexity of JavaScript syntax (which while fairly simple by programming language standards is still complex), 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. 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? :) Commented Apr 2, 2011 at 12:39
  • You need to be very careful because this regex can match comment delimiters which are not really comment 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!" Commented Apr 2, 2011 at 15:43
  • Upon closer inspection, I see that you are looking for a specific type of C-comment. Please disregard my previous comment (and see my suggested answer). Commented Apr 2, 2011 at 16:37

7 Answers 7

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-comments 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-comment
    \/\*!       # 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 comment. Here are the results for both matching and non-matching: (For the non-,matching case I simply removed the last / from the comment)

/*!
 * This is the example comment
 * 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 comment), is much less efficent. Note also that the efficiency difference is even more pronounced with longer and longer comments.

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Wow. Clearly you put a lot of work into this answer, and it is a better solution.. but I think it's a case of premature optimization that over complicate the code.
@amikazmi: For anyone who has studied and is familiar with: MRE3, this is the simple solution!
2

* 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

Comments

2

You need to escape the * too:

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

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

1 Comment

Note that /(\/\*!.*\*\/)/m will fail if there is than one comment in the text. (The greedy-dot-star will match everything from the beginning of the first comment to the end of the last comment. For this expression to work, the dot-star must be lazy, i.e. .*?
1

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

Comments

0

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

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.