Im trying to take a javascript out from some code but I can't get the right regex to match the code, another problem is that I just cant take out all <script tags because I want all the other ones to work correctly.

I tried

<?
$_response = '
before
<script type="text/javascript">
<!--
var ctl00_Main_postComment_rqfValidator = document.all ? document.all["ctl00_Main_postComment_rqfValidator"] : document.getElementById("ctl00_Main_postComment_rqfValidator");
ctl00_Main_postComment_rqfValidator.controltovalidate = "ctl00_Main_postComment_commentTextBox";
ctl00_Main_postComment_rqfValidator.errormessage = "Error: You can\'t enter a blank comment.";
ctl00_Main_postComment_rqfValidator.validationGroup = "ValGroup";
ctl00_Main_postComment_rqfValidator.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";
ctl00_Main_postComment_rqfValidator.initialvalue = "";
// -->
</script>
after';
$_response = preg_replace('/<script type="text\/javascript">\n<!--\nvar ctl00_Main_postComment_rqfValidator(.*?)\/\/ -->\n<\/script>/i', 'javascript cleared' , $_response);

echo $_response;
?>

But it seams the preg_replace doesn't match that expression. How could I make it match "<script type="text/javascript"><!--var ctl00_Main_postComment_rqfValidator" until "</script>" but not take all other javascript off the page?

Thanks for any help 🙂

    Why don't you simpify this and just create a id="foobar" inside the script tag, that why all you have to match is the <script id="foobar"

      ShawnK wrote:

      Why don't you simpify this and just create a id="foobar" inside the script tag, that why all you have to match is the <script id="foobar"

      I made this test script so I didn't have to load the whole pages' html. But im not the one setting the variable for $_response, its loaded from another site.

        $text = preg_replace('#<script type="text/javascript">\s*<!--\s*var ctl00_Main_postComment_rqfValidator.*</script>#iUs', '', $text);
        
          NogDog wrote:
          $text = preg_replace('#<script type="text/javascript">\s*<!--\s*var ctl00_Main_postComment_rqfValidator.*</script>#iUs', '', $text);
          

          God I love you! Thanks

            Write a Reply...