Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl parsing JavaScript file regex, to catch quotes only at the beginning and end of the returned string

Tags:

regex

perl

I'm just starting to learn Perl. I need to parse JavaScript file. I came up with the following subroutine, to do it:

sub __settings {
    my ($_s) = @_;
    my $f = $config_directory . "/authentic-theme/settings.js";
    if ( -r $f ) {
        for (
            split(
                '\n',
                $s = do {
                    local $/ = undef;
                    open my $fh, "<", $f;
                    <$fh>;
                    }
            )
            )
        {
            if ( index( $_, '//' ) == -1
                && ( my @m = $_ =~ /(?:$_s\s*=\s*(.*))/g ) )
            {
                my $m = join( '\n', @m );
                $m =~ s/[\'\;]//g;
                return $m;
            }
        }
    }
}

I have the following regex, that removes ' and ; from the string:

s/[\'\;]//g;

It works alright but if there is a mentioned chars (' and ;) in string - then they are also removed. This is undesirable and that's where I stuck as it gets a bit more complicated for me and I'm not sure how to change the regex above correctly to only:

  1. Remove only first ' in string
  2. Remove only last ' in string
  3. Remove ont last ; in string if exists

Any help, please?

like image 376
Ilia Avatar asked Feb 11 '23 04:02

Ilia


1 Answers

You can use the following to match:

^'|';?$|;$

And replace with '' (empty string)

See DEMO

like image 62
karthik manchala Avatar answered May 15 '23 19:05

karthik manchala