CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
How can I strip javascript

 



HR
Novice

Feb 3, 2001, 3:05 PM

Post #1 of 6 (2684 views)
How can I strip javascript Can't Post

from a file I am copying? Want to take an onload command out of the body tag. This does not seem to work:

if (-e "$basedir/$board/messages/$number.$ext") {
open (COPY, "$basedir/$board/messages/$number.$ext");
@file = <COPY>;
foreach $line (@file) {
if ($line =~ / onLoad="loadFormFields()">$/i) {
$line =~ s/ onLoad="loadFormFields()">$/>/i;
}
}
}



Jasmine
Administrator

Feb 3, 2001, 3:32 PM

Post #2 of 6 (2682 views)
Re: How can I strip javascript [In reply to] Can't Post

You need to escape the parentheses.

Code
if ($line =~ / onLoad="loadFormFields\(\)">$/i) { 
print "\n\nBefore: $line\n";
$line =~ s/ onLoad="loadFormFields\(\)">$/>/i;
print "After: $line\n";
}

Parentheses are used for alternations like (this|that). Example:


Code
$line = 'that yay perl'; 

print "Before: $line\n"; #prints "that yay perl"
$line =~ s/(this|that)//i;
print "After: $line\n"; #prints "yay perl"



HR
Novice

Feb 3, 2001, 4:00 PM

Post #3 of 6 (2680 views)
Re: How can I strip javascript [In reply to] Can't Post

Thank you, that worked. Followup question...... I also need to remove everything following a (!--stop--)* comment line and replace it with (/body)(/html), this approach is not doing what I had hoped, any suggestions?

foreach $line (@file) {

if ($line =~ /(!--stop--)/) {
$line =~ s/$'/(\/body)(\/html)/;
}

}

* Had to substitute parenthesis in place of html brackets so they would show up


Jasmine
Administrator

Feb 4, 2001, 2:13 PM

Post #4 of 6 (2674 views)
Re: How can I strip javascript [In reply to] Can't Post

Remove the $ at the front of the regex. $ means to match at the end of the line. ^ means to match at the beginning of the data.

$line = '<!--stop-->';

$line =~ s|<!--stop-->|</body></html>|;

print $line; # prints </body></html>

You may want to check out the following 2 articles in the Learning Center (from the Perl manual):

Perl Regular Expressions - Quick Start
Perl Regular Expressions - Expanded Tutorial


japhy
Enthusiast / Moderator

Feb 5, 2001, 6:32 AM

Post #5 of 6 (2670 views)
Re: How can I strip javascript [In reply to] Can't Post

No, jasmine, he was using the $' variable, or the $POST_MATCH. In other words, he wants to remove everything after the ";
$EOS = index($string, $marker) + length($marker);
substr($string, $EOS) = "</body></html>";


Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


Jasmine
Administrator

Feb 5, 2001, 8:08 AM

Post #6 of 6 (2669 views)
Re: How can I strip javascript [In reply to] Can't Post

Looks like I missed that lil apostrophe, didn't I? [grin]

Sorry about that confusion.


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives