Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript comment stripper [closed]

I'm looking for some tool to remove cooments from Javascript sources. I was able to Google some, but none of them satisfied the following requirement: Everything else should be left as it is, in particular white space is not removed, BUT if a comment takes a whole line, the line is removed too.

Shortly, I want to be able to go from a nicely formatted source with comments to an equally formatted source without comments. Lines which only contain comments are removed, and traliing comments are removed together with the trailing spaces. All the rest is left as it is.

Do you know any tool for such a job?

EDIT: I try to be more specific. Using regular expressions is not possible, as the characters // or /* can also appear inside strings, regular expressions and so on.

The tool should take this input

var a = true;

//the following code is every useful
var b = 2;//really, really useful
 /**
Never, ever do this
var c = 3;
  */
var d = 4;

and give this output

var a = true;

var b = 2;
var d = 4;
like image 975
Andrea Avatar asked Aug 26 '10 17:08

Andrea


4 Answers

Here's some code I whipped up: Check it out: here

Also here is an example of my code you can test RIGHT NOW in a webpage

Here's one I didn't write that could be handy, though his code will fail on certain regex literals: http://james.padolsey.com/javascript/removing-comments-in-javascript/

EDIT: The code I wrote is as is. I am not updating it as it is something I wrote when I was a teenager and rather new to programming. If there is a bug, you can fix it.

like image 100
Thomas Eding Avatar answered Oct 09 '22 22:10

Thomas Eding


Use Google's Closure Compiler with WHITE_SPACE_ONLY and PRETTY_PRINT -- the only thing that it will do is remove the comments (Unless of course you don't format your code in the way that PRETTY_PRINT does.)

It turns this:

// This function alerts a name
function hello(name) {
    /**
    * One lone
    * multi-line
    * comment
    */
    alert('Hello, ' + name);
}
hello('New user');

Into this:

function hello(name) {
  alert("Hello, " + name)
}
hello("New user");
like image 21
Sean Vieira Avatar answered Oct 09 '22 23:10

Sean Vieira


Found a pretty sweet solution here: http://blog.ostermiller.org/find-comment

Excerpt:

Now we just need to modify the comment end to allow any number of *:

/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/

We now have a regular expression that we can paste into text editors that support regular expressions. Finding our comments is a matter of pressing the find button. You might be able to simplify this expression somewhat for your particular editor. For example, in some regular expression implementations, [^] assumes the [\r\n] and all the [\r\n] can be removed from the expression.

This is easy to augment so that it will also find // style comments:

(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|(//.*)

Be sure to read the caveats, however, as this will remove comments from with comments, or can uncomment commented code improperly. Worked perfectly for me, however :-)

like image 41
mpowered Avatar answered Oct 09 '22 23:10

mpowered


Library decomment does exactly what you described:

Everything else should be left as it is, in particular white space is not removed, BUT if a comment takes a whole line, the line is removed too.

And it also supports JSON5, JavaScript ES6, CSS and HTML.

like image 28
vitaly-t Avatar answered Oct 09 '22 22:10

vitaly-t