http://www.perlmonks.org?node_id=679364


in reply to Removing javascript comments

this is actually fairly complex to do correctly, since javascript has 2 different comment delimiters, and 'comment-like' constructs in string and regex literals (which are especially tricky to recognize) should not be mistaken for comments. You may want to use a full javascript grammar/parser instead of starting from scratch.

If you feel like experimenting, take a look at JE::Parser. Probably not for newbies, though...

Otherwise, you can take advantage of a few facts:

javascript string and regex literals are always single line. This means if you go trough the code line by line, ignoring string and regex literals, you can safely assume** any /* .. */ and // ... constructs left are comments (javascript does not allow for an empty regex literal). As I mentioned before, regex literals will be tricky, since the JS grammar for where regexes are valid (instead of divide operators) are fairly tricky, IOW, you will need to distinguish between

var res = a / 4 /* whatever */ ^^^^^^^^^^^^^^ comment
and
var res = /bla \/* .*/.exec("stuff"); ^^^^^^^ not a comment
for instance.

** in fact you can't, but it's /probably/ good enough.

update: fixed js code.