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:
'
in string'
in string ;
in string if existsAny help, please?
You can use the following to match:
^'|';?$|;$
And replace with ''
(empty string)
See DEMO
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With