asp.net a regular replacement HTML tag with deletion of the specified character method

Source: Internet
Author: User
Tags html tags regular expression tagname

ASP tutorial. NET regular replaces the HTML tag with the deletion of the specified character method, using regular expressions for rule filtering, since HTML tags are based on the <> format and there are symbols like &nbsp;. So it's 2 times. Processing strings to a string that is not HTML-formatted.

Public
string nohtml (string html)
{
String strnohtml = System.Text.RegularExpressions.Regex.Replace (HTML, "<[^>]+>", "");
strnohtml = System.Text.RegularExpressions.Regex.Replace (strnohtml, "&[^;] +;", "");
return strnohtml;
}

Feature Enhancement Code:

public string nohtml (string htmlstring)//Replace HTML tag
{
Delete Script
htmlstring = Regex.Replace (htmlstring, @ "<script[^>]*?>.*?</script>", "", regexoptions.ignorecase);

Delete HTML
htmlstring = Regex.Replace (htmlstring, @) < (. [ ^>]*) > "," ", regexoptions.ignorecase);
htmlstring = Regex.Replace (htmlstring, @ "([RN]) [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 = Regex.Replace (htmlstring, @ "]*>;", "", regexoptions.ignorecase);
Htmlstring.replace ("<", "");
Htmlstring.replace (">", "");
Htmlstring.replace ("RN", "");
htmlstring = HttpContext.Current.Server.HtmlEncode (htmlstring). Trim ();
return htmlstring;
}


Replace strings, regular replacement strings, case-insensitive replacement strings, replace HTML tags, regular matches

Using System;
Using System.Text;
Using System.Text.RegularExpressions;

Namespace EC
{
<summary>
Replace string
</summary>
public class Stringrepstrs
{

Public Stringrepstrs ()
{
}

#region Normal Replacement string

<summary>
Normal replacement string

</summary>
<param name= "src" > Source string </param>
<param name= "pattern" > Regular expression patterns to match </param>
<param name= "Replacement" > Replacement string </param>
<returns> Modified String </returns>
public static string Replace (string src, string pattern, string replacement)
{
Return to Replace (SRC, pattern, replacement, regexoptions.none);
}

#endregion

#region a regular replacement string

<summary>
Regular replacement string

</summary>
<param name= "src" > string to be modified </param>
<param name= "pattern" > Regular expression patterns to match </param>
<param name= "Replacement" > Replacement string </param>
<param name= "Options" > Matching mode </param>
<returns> Modified String </returns>
public static string Replace (string src, string pattern, string replacement, regexoptions options)
{
Regex regex = new Regex (pattern, options|regexoptions.compiled);

return regex. Replace (SRC, replacement);
}

#endregion

#region Case-insensitive replacement string

<summary>
Case-insensitive replacement string

</summary>
<param name= "src" > Source string </param>
<param name= "pattern" > Regular expression patterns to match </param>
<param name= "Replacement" > Replacement string </param>
<returns> Modified String </returns>
public static string Replaceignorecase (string src, string pattern, string replacement)
{
Return to Replace (SRC, pattern, replacement, regexoptions.ignorecase);
}

#endregion


<summary>
Deletes the specified content in the string
</summary>
<param name= "src" > string to be modified </param>
<param name= "pattern" > Regular expression patterns to be deleted </param>
<returns> the string </returns> of the specified content has been deleted
public static string Drop (string src, string pattern)
{
Return to Replace (SRC, pattern, "");
}

<summary>
Deletes the specified content in the string, case-insensitive
</summary>
<param name= "src" > string to be modified </param>
<param name= "pattern" > Regular expression patterns to be deleted </param>
<returns> the string </returns> of the specified content has been deleted
public static string Dropignorecase (string src, string pattern)
{
Return replaceignorecase (SRC, pattern, "");
}

<summary>
Replace string to database Tutorial input mode
</summary>
<param name= "src" > string to insert Database </param>
<returns> A string to insert a database </returns>
public static string Tosql (String src)
{
if (src = null)
{
return null;
}
Return Replace (SRC, "", "");
}

<summary>
Removes the specified HTML tag from the HTML content
</summary>
<param name= "Content" >html contents </param>
<param name= "TagName" >html tags </param>
<returns> Remove the contents of the label </returns>
public static string Drophtmltag (string content, String tagName)
{
Remove <tagname> and </tagname>
Return Dropignorecase (Content, "<[/]{0,1}" + TagName + "[^>]*>");
}

<summary>
Remove all tags from HTML content
</summary>
<param name= "Content" >html contents </param>
<returns> get rid of HTML tag content </returns>
public static string Drophtmltag (string content)
{
Remove <*>
Return Drop (Content, "<[^>]*>");
}

<summary>
Judge whether a data is a number
</summary>
<param name= "Inputdata" > String </param>
<returns> Results </returns>
public static bool IsNumeric (string inputdata)
{
Regex _isnumber = new Regex (@ "^d+{$article $}quot;);
Match m = _isnumber.match (Inputdata);
return m.success;
}

 ///<summary>
 ///Convert HTML tags to web page visible content
 ///</summary>
 ///<param na Me= "src" ></param>
 ///<returns></returns>
  public static string escapehtml ( string src)
  {
   if (src = null)
   {
    return null;
&N bsp; }
   string s = src;
   s = Replace (S, ">", "&gt;");
   s = Replace (S, "<", "&lt;");
   return s;
 }

 ///<summary>
 ///format strings to HTML code
 ///</summary>
 ///<param name= "str" > string to format </param>
 ///<returns> formatted string </returns>
  public static String ToHtml (String str)
  {
   if (str = NULL | | str. Equals (""))
   {
    return str;
  }
           
   StringBuilder sb= new StringBuilder (str);
   sb. Replace ("&", "&amp;");
   sb. Replace ("<", "&lt;");
   sb. Replace (">", "&gt;");
   sb. Replace ("RN", "<br>");
   sb. Replace ("n", "<br>");
   sb. Replace ("T", "");
   sb. Replace ("", "&nbsp;");
   return sb. ToString ();
 }


 ///<summary>
 ///convert HTML code to text Format
 ///</summary>
 ///<param Name= "str" > string to be formatted </PARAM>
 ///<returns> formatted string </returns>
  public static String Totxt (String str)
  {
   if (str = NULL | | str. Equals (""))
   {
    return str;
  }
           
   StringBuilder sb= new StringBuilder (str);
   sb. Replace ("&nbsp;", "");
   sb. Replace ("<br>", "RN");
   sb. Replace ("&lt;", "<");
   sb. Replace ("&gt;", ">");
   sb. Replace ("&amp;", "&");
   return sb. ToString ();
 }
 }
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.