in reply to Re: how to retrieve data between <script></script> and save to an array
in thread how to retrieve data between <script></script> and save to an array

Hi, there, the one you posted following won't work unfortunately

my $left = quotemeta '<fun>'; my $right = quotemeta '</nuf>'; while( $line =~ /$left(.*?)$right/i ){ my $between = $1; }

in my string, there are mixed one line and multiple line between <script> and </script>, how to use regular expression to handle both cases ? Thanks, curd

  • Comment on Re^2: how to retrieve data between <script></script> and save to an array
  • Download Code

Replies are listed 'Best First'.
Re^3: how to retrieve data between <script></script> and save to an array
by Anonymous Monk on Apr 10, 2012 at 08:04 UTC
    Sure it will, just add /s , as in m//si

      thanks for the info, Actually I need to add /g besides smi. Here is the script and it works for the string I provided. Here is the code

      my @script=(); open FILE, "content" or die "Couldn't open file: $!"; while (<FILE>){ $line .= $_; } close FILE; my $left = quotemeta '<script>'; my $right = quotemeta '</script>'; while( $line =~ /$left(.*?)$right/gsmi ){ push (@script, $1); }

      But it will break if $line contains html page with other tags and following <script> contents, for example:

      <html> <head> <script language="javascript" src="" id="scptMDtl"></script> <script language="javascript">var onWL=0;var oJS = new Object();</scri +pt> <script language="javascript"> var a_fInDelItms = 0; var a_fPrv = 1; var a_fTxt = 0; </script> </head> <body> <textarea id="txtBdy" class="w100 txtBdy" style="display:none"> This is a test. </textarea> <script language="javascript"> var a_sId = "RgAAAADz6rvTg9+xRIGTnTWlmKbHBwCeJ0LFThYQQaU\/Nvmo +3DG+AAACNa1IAABO5K\/6RD2SRbSFY4IMoQCbAAAAFHnvAAAJ"; var a_sCK = "TuSv+kQ9kkW0hWOCDKEAmwAAABXdNQ=="; </script> </body> </html>

      could you please to see how to that by giving this long string, Thanks,