开发者

regular expression to remove hidden fields

开发者 https://www.devze.com 2023-02-05 00:39 出处:网络
I have writte开发者_运维知识库n the following regular expression to remove viewstate hidden fields from html

I have writte开发者_运维知识库n the following regular expression to remove viewstate hidden fields from html

Regex.Replace(html, "<input[^>]*id=\"__VIEWSTATE\"[^>]*>", string.Empty, RegexOptions.IgnoreCase);

How can I modify this to include, __EVENTTARGET, __EVENTARGUMENT, __EVENTVALIDATION in the regular expression?


Regex.Replace(html, "<input[^>]*id=\"(__VIEWSTATE|__EVENTTARGET|__EVENTARGUMENT|__EVENTVALIDATION)\"[^>]*>", string.Empty, RegexOptions.IgnoreCase);

Easily understandable and extendable.


You could add all the items to a list, and then iterate through them like this:

var items = new List { "__VIEWSTATE", "__EVENTTARGET", "__EVENTARGUMENT", "__EVENTVALIDATION" };

foreach (item in items) {
    Regex.Replace(html, string.format("<input[^>]*id=\"{0}\"[^>]*>", item), string.Empty, RegexOptions.IgnoreCase);
}

I find this code easier to understand if I came across it in code, as opposed to a collection of OR'd statements.


"<input[^>]*id=\"__(?:VIEWSTATE|(?:EVENT(?:TARGET|ARGUMENT|VALIDATION)))\"[^>]*>"

0

精彩评论

暂无评论...
验证码 换一张
取 消