我得到了下面的StripHTMLTags函数代码,它在VBSCript中工作正常,现在我想要写同样的函数C#
Function StripHTMLTags(ByVal sHTML)
Dim objRegExp, sOutput
sHTML = Replace(Replace(Trim(sHTML & ""), "<", "<"), ">", ">") ' ** PREVENT NULL ERRORS **
If Len(sHTML) > 0 Then
Set objRegExp = New RegExp
With objRegExp
.IgnoreCase = True
.Global = True
.Pattern= "<[^>]+>"
' ** REPLACE ALL HTML TAG MATCHES WITH THE EMPTY STRING **
sOutput = .Replace(sHTML, "")
End With
Set objRegExp = Nothing
StripHTMLTags = sOutput
Else
StripHTMLTags = ""
End If
End Function
请建议,因为这让我很困惑。
答案 0 :(得分:1)
您是否尝试过 Regex.Replace ?
示例:
static string stripHTMLTags1(string html)
{
string pattern = @"<[^>]+>";
var expression = new Regex(pattern);
return expression.Replace(html, String.Empty);
}
static string stripHTMLTags2(string html)
{
// From http://gskinner.com/RegExr/
string pattern = @"</?\w+((\s+\w+(\s*=\s*(?:"".*?""|'.*?'|[^'"">\s]+))?)+\s*|\s*)/?>";
var expression = new Regex(pattern);
return expression.Replace(html, String.Empty);
}
答案 1 :(得分:0)