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;
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.
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");
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 :-)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With