Skeeve has asked for the wisdom of the Perl Monks concerning the following question:

A colleague came up with the question whether I could give him a script removing JavaScript Comments.

Here is what I gave him after discussing that he didn't use regexes in his JS programs (so no need to take car of // being somthing else than a comment).

Maybe someone more perlish than me likes to add his or her comments to it!?

#!perl use strict; my $qs =qr/'(?:[^']|\\')*'/; my $qqs=qr/"(?:[^"]|\\")*"/; my $ncs=qr<(?:[^'"/]|/[^/*])>; #' my $nocomment=qr/(?:$ncs|$qqs|$qs)*/; my $hit; while (<>) { # remove multiline /* comment if ($hit= s{^($nocomment)/\*(?:[^*]|\*[^/])*$}{$1}o ... s{^.*? +\*/}{}) { redo if $hit=~ /E0/i; $_="\n" if $hit>1; print; next; } # remove one-line /*...*/ comment 1 while s{^($nocomment)/\*.*?\*/}{$1}o; # remove // comment s{^($nocomment)//.*}{$1}o; print; }

Replies are listed 'Best First'.
Re: strip JS-comments
by antirice (Priest) on Jun 10, 2003 at 18:43 UTC

    Not trying to be picky but:

    var bar = "Use \"//\" to make single-line comments" window.alert(bar)


    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1

      Ooops!

      You are absolutely right. So how can I update my code above, except from stating here:

      Update (thanks to antirice):

      my $qs =qr/'(?:[^\\']|\\.)*'/; my $qqs=qr/"(?:[^\\"]|\\.)*"/;
Re: strip JS-comments
by allolex (Curate) on Jun 11, 2003 at 15:19 UTC

    Update: Doh! Misconstrued the question to mean remove JavaScript. Leaving the code anyway---it might be useful to someone.

    Aristotle helped me with a similar project a while back in another forum. Here's the result. It uses HTML::TokeParser::Simple to do the stripping and works in a very intuitive manner.

    #!/usr/bin/perl use strict; use HTML::TokeParser::Simple;

    --
    Allolex

Re: strip JS-comments
by diotalevi (Canon) on Jun 10, 2003 at 13:40 UTC
      You haven't tried, have you!?

        No, I took your text at its word. Your documentation is wrong?