C Regular Filtering of HTML Tags and Retaining Specified Tags

  • 2021-12-21 04:45:15
  • OfStack

This article illustrates how C # regularly filters HTML tags and retains the specified tags. Share it for your reference, as follows:

I mainly see one filtering function here:


public static string FilterHtmlTag(string s)
{
 //<...> Marked regular expression 
 return Regex.Replace(s, @"<[^>]*>", delegate(Match match)
 {
  string v = match.ToString();
  // Picture ,<p>,<br> Regular expression 
  Regex rx = new Regex(@"^<(p|br|img.*)>$",
   RegexOptions.Compiled | RegexOptions.IgnoreCase); //
  if (rx.IsMatch(v))
  {
  return v; // Keep pictures ,<p>,<br>
  }
  else
  {
  return ""; // Filter out 
  }
 });
}

My side filters everything, so I just use regular instead of keeping p and br for anonymous delegates


content = Regex.Replace(content, @"/\<span(\sclass\=\S*)*\>\S*\<\/span\>/g", "", RegexOptions.IgnoreCase);
content = Regex.Replace(content, @"<[^>]*>", "", RegexOptions.IgnoreCase);
content = content + " . . . ";

PS: Here are two very convenient regular expression tools for your reference:

JavaScript Regular Expression Online Test Tool:
http://tools.ofstack.com/regex/javascript

Regular expression online generation tool:
http://tools.ofstack.com/regex/create_reg

For more readers interested in C # related content, please check the topics on this site: "C # Regular Expression Usage Summary", "C # Coding Operation Skills Summary", "C # Common Control Usage Tutorial", "WinForm Control Usage Summary", "C # Data Structure and Algorithm Tutorial", "C # Object-Oriented Programming Introduction Tutorial" and "C # Programming Thread Usage Skills Summary"

I hope this article is helpful to everyone's C # programming.


Related articles: