【问题标题】:How to remove extra attributes of img tag using regular expression?如何使用正则表达式删除 img 标签的额外属性?
【发布时间】:2016-03-06 17:42:24
【问题描述】:

我有一个使用过时文本编辑器的 Web 表单。这样,每当用户在设计视图中插入图像时,html 视图都会生成 2 个宽度/高度属性。当它发布到服务器时,它会导致使用 System.Xml.XPath.XPathDocument 的其他页面出现问题。

由于某些原因,我无法用现代的文本编辑器替换文本编辑器,因此希望从服务器端发布的 img 标记中删除额外的属性(宽度和高度)。

以下是发布到服务器的html文本示例:

<table align="left">
    <thead>
    </thead>
    <tbody>
        <tr>
            <td style="width: 250px; vertical-align: top; text-align: left;">
            <p>&nbsp;<img width="263" height="175" alt="" style="height: 122px; width: 184px;" src="/Portals/0/Resources/Site/Signature/1.PNG" width="263" height="175" /></p>
            </td>
            <td style="width: 50px;">&nbsp;</td>
            <td style="width: 250px; vertical-align: top; text-align: left;">
            <p>&nbsp;</p>
            <p><img width="168" height="66" alt="" style="height: 79px; width: 170px;" src="/Portals/0/Resources/Site/Signature/2.jpg" width="168" height="66" /></p>
            </td>
            <td style="width: 50px;">&nbsp;</td>
            <td style="width: 250px; vertical-align: top; text-align: left;">
            <p>&nbsp;</p>
            <p><img width="217" height="93" alt="" src="/Portals/0/Resources/Site/Signature/3.png" width="217" height="93" /></p>
            </td>
        </tr>
    </tbody>
</table>

有没有使用 vb.net 正则表达式删除它们的有效方法?或者,您有更好的处理方法?

【问题讨论】:

  • 是的,有一个更好的方法 - 使用 HTMLAgilityPack 之类的 HTML 解析器。
  • 感谢 Fᴀʀʜᴀɴ Aɴᴀᴍ。 HTMLAgilityPack 完成了这项工作!

标签: html regex vb.net


【解决方案1】:

使用 HTMLAgilityPack 删除重复的属性(VB.NET):

     Dim txtTemp As string = ""
     Try
         Dim htmlDocument as HtmlDocument = New HtmlDocument()
         htmlDocument.LoadHtml(txtPosted)             
         for each imgNode as HtmlNode In htmlDocument.DocumentNode.Descendants("img")
    'Get value of width/height attribute
             Dim txtTempWidth as string = IIf(imgNode.Attributes.Contains("width"), imgNode.Attributes("width").Value, "").ToString()
             Dim txtTempHeight as string = IIf(imgNode.Attributes.Contains("height"), imgNode.Attributes("height").Value, "").ToString()                 
             if not string.IsNullOrEmpty(txtTempWidth)  then
         'remove all "width" attributes
                 While imgNode.Attributes.Contains("width")
                     imgNode.Attributes.Remove("width")
                 End While
         'add one "width" attribute
                 imgNode.Attributes.Add("width", txtTempWidth)
             End If
             if not string.IsNullOrEmpty(txtTempHeight)  then
         'remove all "height" attributes
                 While imgNode.Attributes.Contains("height")
                     imgNode.Attributes.Remove("height")
                 End While  
         'add one "height" attribute
                 imgNode.Attributes.Add("height", txtTempHeight)
             End If     
         Next

     'close img tag
         if (HtmlNode.ElementsFlags.ContainsKey("img")) then
            HtmlNode.ElementsFlags("img") = HtmlElementFlag.Closed
         else
            HtmlNode.ElementsFlags.Add("img", HtmlElementFlag.Closed)
         end if

        using writer as StringWriter = new StringWriter()
            htmlDocument.Save(writer)
            txtTemp = writer.ToString()
        End Using
     Catch ex As Exception
        Exceptions.LogException(ex)
        txtTemp = ""
     End Try


    'final result
    Dim txtFinal as string = IIf(string.IsNullOrEmpty(txtTemp), txtPosted, txtTemp) .ToString()

【讨论】:

    猜你喜欢
    • 2018-02-08
    • 2011-04-18
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 2011-07-27
    • 1970-01-01
    相关资源
    最近更新 更多