Can somebody put a regex expression that will:
- find a chunk that starts with [% and ends with %]
- within that chunk replace all xml special characters with:
& quot; & apos; & lt; & gt; & amp; - leave everything between <%= %> or <%# %> as is except make sure that there is space after <%# or <%= and before %> for example <%=Integer.MaxValue%> should become <%= Integer.MaxValue %>
source:
[% 'test' <mtd:ddl id="asdf" runat="server"/> & <%= Integer.MaxValue% > %]
result:
'test' <mtd:ddl id="asdf" runat="server"/> & <%= Integer.MaxValue %>
Used 2 regular expressions. 1st to match the general form, 2nd to deal with the inner plumbing.
For the XML encoding I used an obscure little method found in System.Security: SecurityElement.Escape Method. I fully qualified it in the code below for emphasis. Another option would be using the HttpUtility.HtmlEncode method but that may involve a reference to System.Web depending on where you’re using this.
Results:
EDIT: if you don’t care to preserve the opening/closing [%%] in the final result then change the pattern to:
Then be sure to remove references to
m.Groups["open"].Valueandm.Groups["close"].Value.