in reply to Re: strip JS-comments
in thread strip JS-comments

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/"(?:[^\\"]|\\.)*"/;
The complete code (I also added some enhancements/bug-fixes after testing with my colleague)...
#!perl use strict; my $qs =qr/'(?:[^\\']|\\.)*'/; my $qqs=qr/"(?:[^\\"]|\\.)*"/; my $ncs=qr<(?:[^'"/]|/[^/*])>; #' my $nocomment=qr/(?:$ncs|$qqs|$qs)*/; my $hit; while (<>) { s/[\r\n]//g; # remove multiline /* comment if ($hit= s{^($nocomment)/\*(?:[^*]|\*[^/])*$}{$1}o ... s{^.*? +\*/}{}) { redo if $hit=~ /E0/i; $_='' if $hit>1; print $_,"\n"; next; } # remove one-line /*...*/ comment redo if s{^($nocomment)/\*.*?\*/}{$1}o; # remove // comment s{^($nocomment)//.*}{$1}o; print $_,"\n"; }