去除javascript注释

#!/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];
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值