views:

92

answers:

2

I'm looking to create a perl script that I will run on a JavaScript file to automatically change ( ' ) that breaks the script.

Example:

file.js

document.writeln('&#187; <a href="/LINK1" TARGET="_blank">Lorem ipsum lorem 1</a><br>');

document.writeln('&#187; <a href="/LINK2" TARGET="_blank">Lorem ipsum lor'em x em 2</a><br>');

document.writeln('&#187; <a href="/LINK3" TARGET="_blank">Lorem ipsum lorem 3</a><br>');

In 2nd line " Lorem ipsum lor'em x em 2 " contains a single quote which will be removed by script. Rest of the single quotes will be there like " document.writeln(' "

A: 

The simplest way would be to replace all the ' and then replace back the safe ones, something along the lines of:

s/'/\\'/g; # replace all single quotes
s/document.writeln(\\'/document.writeln('/g; # revert safe occurrences 

Obviously such a solution is a quick and dirty fix that will work only if you have control on your input and will fail miserably if the input format isn't well known.

Motti
+3  A: 

Try following regular expression:

$data =~ s/
    (?<!   # negative look-behind
        \( #   ensure no open parenthesis behind
    )
    '      # a quote mark
    (?!    # negative look-ahead
        \) #   ensure no close parenthesis ahead
    )
/\\'/xsg;

It will take your second line:

document.writeln('&#187; <a href="/LINK2" TARGET="_blank">Lorem ipsum lor'em x em 2</a><br>');

and output:

document.writeln('&#187; <a href="/LINK2" TARGET="_blank">Lorem ipsum lor\'em x em 2</a><br>');

A simple script might be:

while ( <> ) {
    $_ =~ ... # regular expression given above
    print $_;
}

You would run this by typing:

perl myscript.pl file.js
PP
I'm sorry, I'm not downloading a random .zip file. And I'm not doing your homework either. I've shown you a path, and it is up to you to decide if it is a path you want to take.
PP