Okay, looked at this for a challenge - and it is.
Best I could do was:
<?
$str = "
hello
hi
script
some lines
hi again
script
more lines
and more...
";
echo preg_replace( "/(?<=script(.))\n(?=(.)script)/", '<br>', $str);
?>
(?<=script(.))\n(?=(.)script)
This means:
(?<= if Looking back
script(.) I match script then anything
\n and now a new line
(?= and looking forward
(.)script I match something then 'script'
then match.
This doesn't work though, as the look back does not have a fixed length. Perl only supports fixed length look backs.
Best bet, as suggested above, swap all \n to <br>, then back the <br> in a script tags and make them \n again. Or you might be able to use the above, modify it in some way.