ASP. NET regular expression deletes the code for the specified HTML tag

Source: Internet
Author: User
If you delete the HTML tags, it may be difficult to read (such as A, IMG tags), it is best to delete part of the reservation part.

In regular expressions, it is very easy to understand that some strings are included, but it is really a puzzle to know how to tell if you don't include some strings (strings, not characters, some, not some).

< (?! ((/?\s?li) | (/?\s?ul) | (/?\s?a) | (/?\s?img) | (/?\S?BR) | (/?\s?span) | (/?\s?b))) [^>]+>

The regular is to determine that the HTML tag does not contain li/ul/a/img/br/span/b, as far as the above requirements, is to remove the HTML tag except the one listed here, which I groped for a long time to get out.
(?! EXP) match the location followed by the EXP
/?\s? I initially tried to write it to the front <, but the test failed.

Here is a simple function, the tag to keep the string up, generate a regular expression, and then delete the unwanted tag ...

private static string removespecifyhtml (String ctx) {string[] Holdtags = {"A", "IMG", "BR", "strong", "B", "span"};//to be insured Left tag//< (?!) ((/?\s?li) | (/?\s?ul) | (/?\s?a) | (/?\s?img) | (/?\S?BR) | (/?\s?span) | (/?\s?b))) [^>]+> string regstr = string. Format @ < (?! (/?\s? {0}))) [^>]+> ", String. Join (@ ") | (/?\s? ", Holdtags)); Regex reg = new Regex (Regstr, regexoptions.compiled | Regexoptions.multiline | Regexoptions.ignorecase); Return Reg. Replace (CTX, "");}

Correction:
Above the regular, if the Li is retained, the actual operation will find that link is also retained, the retention of a will be addr also to retain, the solution is to add \b assertion.

< (?! ((/?\s?li\b) | (/?\s?ul) | (/?\s?a\b) | (/?\s?img\b) | (/?\s?br\b) | (/?\s?span\b) | (/?\s?b\b))) [^>]+> private static string removespecifyhtml (String ctx) {string[] Holdtags = {"A", "IMG", "BR", "strong", "B", "Span", "li"};//reserved Tag//< (?! ((/?\s?li\b) | (/?\s?ul\b) | (/?\s?a\b) | (/?\s?img\b) | (/?\s?br\b) | (/?\s?span\b) | (/?\s?b\b))) [^>]+> string regstr = string. Format @ < (?! (/?\s? {0}))) [^>]+> ", String. Join (@ "\b) | (/?\s? ", Holdtags)); Regex reg = new Regex (Regstr, regexoptions.compiled | Regexoptions.multiline | Regexoptions.ignorecase); Return Reg. Replace (CTX, "");} 
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.