#!/usr/bin/perl
use strict;
my $script1 = q(var regexp1 = /^[abc"//>0]+$/; alert(regexp1.test('a0bc>a/b"bc//a"0c>cb')); var str1 = 'public String aaa = "abc//efg"; // return "aaa";//pubKick();'; // this is the real comment1"cc"bb//a"/aa/*""cc);
my $script2 = q(var regexp2 = /^[abc/"//>0]+$/; alert(regexp2.test('a0bc>a/b"bc//a"0c>cb')); var str2 = 'public String aaa = "abc//efg"; // return "aaa";//pubKick();'; // this is the real comment2"cc"bb//a"/aa/*""cc);
my $script3 = q(var regexp3 = /^[abc/"//>0]+$/; alert(regexp3.test('a0bc>a/b"bc//a"0c>cb')); var str3 = 'public String aaa = "abc//efg"; // return "aaa";//pubKick();'; // this is the real comment3"cc"bb//a"/aa/*""cc);
my $tidy = &remove_comments($script3);
print "$tidy/n";
sub remove_comments {
my $str = shift;
my @characters = split //, '__'.$str.'__';
my %mode = (
dobule_quote => 0,
single_quote => 0,
regex => 0,
line_comment => 0,
block_comment => 0,
cond_comp => 0
);
foreach my $position ( 0 .. $#characters-1 ) {
if ( $mode{dobule_quote} ) {
if ( $characters[$position] eq '"' && $characters[$position-1] ne '//' ) {
$mode{dobule_quote} = 0;
}
next;
}
if ( $mode{single_quote} ) {
if ( $characters[$position] eq "'" && $characters[$position-1] ne '//' ) {
$mode{single_quote} = 0;
}
next;
}
if ( $mode{regex} ) {
if ( $characters[$position] eq '/' && $characters[$position-1] ne '//' ) {
$mode{regex} = 0;
}
next;
}
if ( $mode{line_comment} ) {
if ( $characters[$position+1] eq '/n' || $characters[$position+1] eq '/r') {
$mode{line_comment} = 0;
}
$characters[$position] = '';
next;
}
if ( $mode{block_comment} ) {
if ( $characters[$position] eq '*' && $characters[$position+1] eq '/') {
$mode{block_comment} = 0;
}
$characters[$position] = '';
next;
}
if ( $mode{cond_comp} ) {
if ( $characters[$position-2] eq '@' && $characters[$position-1] eq '*' && $characters[$position] eq '/' ) {
$mode{cond_comp} = 0;
}
next;
}
$mode{dobule_quote} = 1 if $characters[$position] eq '"';
$mode{single_quote} = 1 if $characters[$position] eq "'";
if ( $characters[$position] eq '/' ) {
if ( $characters[$position+1] eq '/' ) {
$characters[$position] = '';
$mode{line_comment} = 1;
next;
}
if ( $characters[$position+1] eq '*' ) {
$characters[$position] = '';
$mode{block_comment} = 1;
next;
}
if ( $characters[$position+1] eq '*' && $characters[$position+2] eq '@' ) {
$mode{cond_comp} = 1;
next;
}
$mode{regex} = 1;
}
}
return join '', @characters[2..$#characters-2];
}
去除javascript注释
最新推荐文章于 2022-10-28 18:24:51 发布