在ASP语言中,如果要在代码中加入HTML标记,可以使用下列方法:
直接在代码中加入HTML标记,需要注意的是要在所有的HTML标记前面加入%。例如:
<%
Response.Write("<p>这是一段段落</p>")
%>
使用Response.Write()函数来输出HTML标记。例如:
Response.Write("<p>这是一段段落</p>")
使用Response.Write()函数输出带参数的HTML标记。例如:
Response.Write("<p class='text-primary'>这是一段段落</p>")
使用Response.Write()函数输出用单引号包含的HTML标记。例如:
Response.Write("<p class='text-primary'>这是一段段落</p>")
使用Response.Write()函数输出用双引号包含的HTML标记。例如:
Response.Write("<p class="text-primary">这是一段段落</p>")
使用Response.Write()函数输出用单引号包含的带参数的HTML标记。例如:
Response.Write("<p class='text-primary'>这是一段段落</p>")
using System.Text.RegularExpressions
/// <summary>
///去除HTML标记
/// </summary>
/// <paramname="NoHTML">包括HTML的源码 </param>
/// <returns>已经去除后的文字</returns>
public static stringNoHTML(stringHtmlstring)
{
//删除脚本
Htmlstring =Regex.Replace(Htmlstring,@"<script[^>]*?>.*?</script>","",RegexOptions.IgnoreCase)
//删除HTML
Htmlstring =Regex.Replace(Htmlstring,@"<(.[^>]*)>","",RegexOptions.IgnoreCase)
Htmlstring =Regex.Replace(Htmlstring,@"([\r\n])[\s]+","",RegexOptions.IgnoreCase)
Htmlstring =Regex.Replace(Htmlstring,@"-->","",RegexOptions.IgnoreCase)
Htmlstring =Regex.Replace(Htmlstring,@"<!--.*","",RegexOptions.IgnoreCase)
Htmlstring =Regex.Replace(Htmlstring,@"&(quot|#34)","\"",RegexOptions.IgnoreCase)
Htmlstring =Regex.Replace(Htmlstring,@"&(amp|#38)","&",RegexOptions.IgnoreCase)
Htmlstring =Regex.Replace(Htmlstring,@"&(lt|#60)","<",RegexOptions.IgnoreCase)
Htmlstring =Regex.Replace(Htmlstring,@"&(gt|#62)",">",RegexOptions.IgnoreCase)
Htmlstring =Regex.Replace(Htmlstring,@"&(nbsp|#160)"," ",RegexOptions.IgnoreCase)
Htmlstring =Regex.Replace(Htmlstring,@"&(iexcl|#161)","\xa1",RegexOptions.IgnoreCase)
Htmlstring =Regex.Replace(Htmlstring,@"&(cent|#162)","\xa2",RegexOptions.IgnoreCase)
Htmlstring =Regex.Replace(Htmlstring,@"&(pound|#163)","\xa3",RegexOptions.IgnoreCase)
Htmlstring =Regex.Replace(Htmlstring,@"&(copy|#169)","\xa9",RegexOptions.IgnoreCase)
Htmlstring =Regex.Replace(Htmlstring, @"(\d+)","",RegexOptions.IgnoreCase)
Htmlstring.Replace("<","")
Htmlstring.Replace(">","")
Htmlstring.Replace("\r\n","")
Htmlstring=HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim()
returnHtmlstring
}
写一个静态方法
#region移除HTML标签
/// <summary>
///移除HTML标签
/// </summary>
/// <paramname="HTMLStr">HTMLStr</param>
public static string ParseTags(stringHTMLStr)
{
returnSystem.Text.RegularExpressions.Regex.Replace(HTMLStr, "<[^>]*>", "")
}
#endregion
#region取出文本中的图片地址
/// <summary>
///取出文本中的图片地址
/// </summary>
/// <paramname="HTMLStr">HTMLStr</param>
public static stringGetImgUrl(stringHTMLStr)
{
stringstr = string.Empty
stringsPattern = @"^<img\s+[^>]*>"
Regexr = newRegex(@"<img\s+[^>]*\s*src\s*=\s*([']?)(?<url>\S+)'?[^>]*>",
RegexOptions.Compiled)
Matchm =r.Match(HTMLStr.ToLower())
if(m.Success)
str =m.Result("${url}")
returnstr
}
#endregion