开发者

Simple Perl script help required

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(' "


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜