admin管理员组

文章数量:1433902

Here is my dilemna:

I am a noob (currently interning and helping to maintain two e-merce sites) at javascript. I was recently assigned to remove all the ments that occur in our javascript libraries (which is over 25,000 ments!). Obviously I want to find a function or some pre-existing program that can parse through the code, removing all characters following // or */...

I have looked into some minifiers available online such as Yui, jspressor, and uglifyJS that would make this task more automated, but there are a few problems. Either they are too aggressive (shortening variable names, removing all whitespace, etc.) or they require that you feed one line or one file at a time. I am dealing with literally 1000s of .js files.

Additional details: our development environment is Eclipse IDE and xammp; languages are html, php, css.

Any remendations of a program that can fit my needs would be great!

Here is my dilemna:

I am a noob (currently interning and helping to maintain two e-merce sites) at javascript. I was recently assigned to remove all the ments that occur in our javascript libraries (which is over 25,000 ments!). Obviously I want to find a function or some pre-existing program that can parse through the code, removing all characters following // or */...

I have looked into some minifiers available online such as Yui, jspressor., and uglifyJS that would make this task more automated, but there are a few problems. Either they are too aggressive (shortening variable names, removing all whitespace, etc.) or they require that you feed one line or one file at a time. I am dealing with literally 1000s of .js files.

Additional details: our development environment is Eclipse IDE and xammp; languages are html, php, css.

Any remendations of a program that can fit my needs would be great!

Share Improve this question asked May 1, 2012 at 19:27 user1183433user1183433 711 silver badge3 bronze badges 7
  • 14 Why on earth would you want to remove ments but not minify? – Evan Davis Commented May 1, 2012 at 19:29
  • 2 hm. is it possible to script a reg ex for line endings. PERL could definately do that (I think). – r4. Commented May 1, 2012 at 19:29
  • 2 jspress. should do it for you.. There are tons of other minifiers online for both js and css. It's a good practice to keep the original file with indentation and ments and minify the file which is going to production. – MilkyWayJoe Commented May 1, 2012 at 19:29
  • The number of files isn't really material; simple scripting deals with that. – Dave Newton Commented May 1, 2012 at 19:34
  • 1 YUI seems to have a --nomunge option to disable obfuscation. github./yui/yuipressor/blob/master/doc/README They also have --disable-optimizations to avoid other code changes. The result will still be minified, but perhaps you could just run the result through jsbeautifier to restore your indentation. – user1106925 Commented May 1, 2012 at 19:37
 |  Show 2 more ments

3 Answers 3

Reset to default 2

Take a closer look at uglifyjs. It neither presses nor munges by default (you have to give the -c and -m options, respectively), and you can choose in fine detail what kinds of pression it does, even to the level of specifying a regular expression for what kinds of ments to remove. And, you can even pretty print on the way out, if you're so inclined. So what's the problem with using it?

I know this question is a few years old - but all the Javascript ment strippers I found couldn't handle the 2.6mb Javascript file I was trying to strip.

I created a jsfiddle with the following code, then pasted the 2.6mb file into the textbox and it worked for me:

$("textarea").val($("textarea").val().replace(/\/\*([^*]|[\r\n]|(\*+([^*\/]|[\r\n])))*\*+\//g,"")); /*remove these ment types*/

$("textarea").val($("textarea").val().replace(/\/\/.*/g,"")); // remove these ment types

https://jsfiddle/40okonqo/

Hope it helps someone.

Credit: I used information found here to help with the regular expression: http://blog.ostermiller/find-ment

In fact, it is not that easy to build a regexp that removes all ments from a javascript document.

The basic solution is to use :

     yourJavascriptString.replace(/\/\*.+?\*\/|\/\/.*(?=[\n\r])/g, '');

Unfortunately it does not always works. If you need a more plete solution, please visit this website : http://james.padolsey./javascript/removing-ments-in-javascript/

本文标签: Looking to remove comments from a large amount of javascript filesStack Overflow