Regular expression to replace including new line

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jambalapamba
    New Member
    • Nov 2007
    • 23

    Regular expression to replace including new line

    Hi

    I am trying to replace anything between <script> </script> tags including script, I am using the following regular expression

    Regex.Replace(i nput, @"<script\b[^>]*>(.*?)<\ /script>", " ");

    But the problem is if there is a newline
    e.g:- <scirpt> sample
    </script>

    Its not working because "." matches except new line. Can any one help me with this


    Thank You
  • Robert Selzer
    New Member
    • Jan 2008
    • 5

    #2
    Originally posted by jambalapamba
    Hi

    I am trying to replace anything between <script> </script> tags including script, I am using the following regular expression

    Regex.Replace(i nput, @"<script\b[^>]*>(.*?)<\ /script>", " ");

    But the problem is if there is a newline
    e.g:- <scirpt> sample
    </script>

    Its not working because "." matches except new line. Can any one help me with this


    Thank You
    Try this
    Regex.Replace(i nput, "<script[^>]*>(.|\n)*</script[^>]*>", "");

    Comment

    • Robert Selzer
      New Member
      • Jan 2008
      • 5

      #3
      Or if you want to keep the tags:
      Regex.Replace(s trX, "<script[^>]*>(.|\n)*</script[^>]*>", "<script></script>");

      Comment

      • jambalapamba
        New Member
        • Nov 2007
        • 23

        #4
        i found the answer using regex options which is very useful

        string regex = "<script(.* ?)</script>";
        RegexOptions options = (RegexOptions.I gnorePatternWhi tespace | RegexOptions.Si ngleline | RegexOptions.Ig noreCase);
        Regex reg = new Regex(regex, options);
        string rep= reg.Replace(inp ut, "anil");

        Comment

        Working...