asp.net regular expression deletes the code for the specified HTML tag

Source: Internet
Author: User
Tags format copy expression html tags join net regular expression string
If you delete all of the HTML tags inside, you may have difficulty reading (such as A, IMG tags), preferably by deleting a part and retaining part of it.

In regular expressions, it is very easy to understand that it is quite understandable to include certain strings, but it is really a mystery how to judge that there are no strings (strings, not characters, some, not some).
Copy CodeThe code is as follows:
< (?! ((/?\s?li) (/?\s?ul) (/?\s?a) (/?\s?img) (/?\S?BR) (/?\s?span) (/?\s?b)) [^>]+>

This is to determine that the HTML tag does not contain li/ul/a/img/br/span/b, in terms of the above requirements, is to delete the HTML tags listed here, which I have been groping for a long time to get out.
(?! EXP) match the position that follows not exp
/?\s? I first tried to write it to the front <, but the test failed.

Here is a simple function, to keep the tag string up, generate a regular expression, and then delete the unwanted tag ...
Copy CodeThe code is as follows:
private static string removespecifyhtml (String ctx) {
String[] Holdtags = {"A", "IMG", "BR", "strong", "B", "span"};//to keep the 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 you keep Li, the actual operation will find link also to retain, keep a will addr also to retain, the solution is to add \b assert.
Copy CodeThe code is as follows:
< (?! ((/?\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.