1

我使用下面的正则表达式替换两个单词之间的文本。它有效,只是它跳过了其中一些。下面粘贴的是一个示例。

var EditedHtml = Regex.Replace(htmlText, @"<script(.*?)</script>", ""); 

html文本:

 <head>
   <script src=" https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
   <script src=" https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js" type="text/javascript"></script>
   <script src="/AspellWeb/v2/js/dragiframe.js" type="text/javascript"></script>
   <script type="text/javascript">
     var applicationName = '/';
     FullPath = (applicationName.length > 1) ? 'http://localhost:65355' + applicationName : 'http://localhost:65355';
     //FullPath = 'http://localhost:65355';
     GetPath = function (url) {
     return FullPath + url;
   }
   </script>

   <script type="text/javascript" src="../../Scripts/stats.js?"></script>
</head>

<body>
  .......
  <script type="text/javascript">
    function loadAndInit() {

    $(".dvloading").hide();
    if ($.browser.mozilla) {
      if (location.pathname == "/Stats/Reports") {            // This is for local env.
        $("#prntCss").attr("href", "../../../Content/SitePrint_FF.css");
      }
      else {                                                  // This is for DEV/QA/STAGE/PROD env. 
        $("#prntCss").attr("href", "../../Content/SitePrint_FF.css");
      }
    }

  }
  </script>
</body>

编辑的HTML:

<head>
  <script type="text/javascript">
    var applicationName = '/';
    FullPath = (applicationName.length > 1) ? 'http://localhost:65355' + applicationName : 'http://localhost:65355';
    //FullPath = 'http://localhost:65355';
    GetPath = function (url) {
      return FullPath + url;
    }
  </script>
</head>

<body>
  .......
  <script type="text/javascript">
    function loadAndInit() {

      $(".dvloading").hide();
      if ($.browser.mozilla) {
        if (location.pathname == "/Stats/Reports") {            // This is for local env.
          $("#prntCss").attr("href", "../../../Content/SitePrint_FF.css");
        }
        else {                                                  // This is for DEV/QA/STAGE/PROD env. 
          $("#prntCss").attr("href", "../../Content/SitePrint_FF.css");
        }
      }

    }
  </script>
</body>
4

4 回答 4

4

为什么要使用 Regex 来解析 html。看到这个

使用像HtmlAgilityPack这样的真正的 html 解析器会容易得多

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.Load(filename); //or doc.LoadHtml(HtmlString)

doc.DocumentNode.Descendants()
    .Where(n => n.Name == "script").ToList()
    .ForEach(s => s.Remove());

StringWriter wr = new StringWriter();
doc.Save(wr);
var newhtml = wr.ToString();
于 2013-04-16T22:46:11.803 回答
2

在单行模式下尝试:

var EditedHtml = Regex.Replace(
    htmlText, @"<script(.*?)</script>", "", 
    RegexOptions.Singleline); 

文档引用:

指定单行模式。更改点 (.) 的含义,使其匹配每个字符(而不是除 \n 之外的每个字符)。

于 2013-04-16T22:30:53.007 回答
2

尝试

var EditedHtml = Regex.Replace(
    htmlText, @"<script(.*?)</script>", "", RegexOptions.Singleline
); 

使用单行模式,以便.匹配任何字符,包括换行符。

于 2013-04-16T22:31:04.993 回答
0

尝试这个:

//(.|\r\n)*: matches every character and/or newline zero or more times
//(.|\r\n)*?: as few times as possible == > you get rid of <script> tags and of their content but you keep the rest of your html
var EditedHtml = Regex.Replace(htmlText, @"<script (.|\r\n)*?</script>", ""); 

希望能帮助到你

参考资料:http: //msdn.microsoft.com/en-us/library/az24scfc.aspx

于 2013-04-16T22:50:22.133 回答