regular expression to remove hidden fields
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)))\"[^>]*>"
精彩评论