From: Guangyi Wu <gua...@al...> - 2000-12-06 11:40:50
|
I just download the JSPack and read the code. It is very very GREAT! Although your code works for most cases, there are some more issues to consider for removing comment. If there are strings containing the comment tokens, your current script will fail. Do you notice my second post on 11.27 with my Perl script which is in fact a rewriten script from another's post? There was a line to strip all comments in JS. This line comes from Perl FAQ 6 to strip C comment and I added the part for //. s#/\*[^*]*\*+([^/*][^*]*\*+)*/|//.*(?=\n)|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|\ n+|.[^/"'\\]*)#$2#g; Let me change it to a more readable way. s{ /\*[^*]*\*+([^/*][^*]*\*+)*/ # comment /*..*/ | //.*(?=\n) # comment // | ( "(\\.|[^"\\])*" | '(\\.|[^'\\])*' | \n+ | .[^/"'\\]* ) # code which should not change # they are "" string, '' string, new lines # and other characters. }{$2}gx It works for following cases: =============================== Test file: test.js, which is actually not a JS file =============================== line 1; // This is a comment // This is a comment // comments with indents line 2; // This is a comment /* line 3; /* This is a comment and // a commment*/ /* short comment */line 4; /* and other comment */ /* short // comment */line 5; /* another comment */ line 6:"not a comment /*"; line 7:"not a comment */"; /* but it is a comment */ line 8:'not a comment //'; // but it is a comment ===============================EOF If you like it, you can add it into the beginning of compressCode(). If it can handle more cases, developers get more free at comment. And I would even suggest to follow Javadoc convention in order to generate reference/document automatically. br George |