Note that there are some explanatory texts on larger screens.

plurals
  1. POConvert > to HTML entity equivalent within HTML string
    primarykey
    data
    text
    <p>I'm trying to convert all instances of the > character to its HTML entity equivalent, &gt;, within a string of HTML that contains HTML tags. The furthest I've been able to get with a solution for this is using a regex.</p> <p>Here's what I have so far:</p> <pre><code> public static readonly Regex HtmlAngleBracketNotPartOfTag = new Regex("(?:&lt;[^&gt;]*(?:&gt;|$))(&gt;)", RegexOptions.Compiled | RegexOptions.Singleline); </code></pre> <p>The main issue I'm having is isolating the single > characters that are not part of an HTML tag. I don't want to convert any existing tags, because I need to preserve the HTML for rendering. If I don't convert the > characters, I get malformed HTML, which causes rendering issues in the browser.</p> <p>This is an example of a test string to parse:</p> <pre><code>"Ok, now I've got the correct setting.&lt;br/&gt;&lt;br/&gt;On 12/22/2008 3:45 PM, jproot@somedomain.com wrote:&lt;br/&gt;&lt;div class"quotedReply"&gt;&gt; Ok, got it, hope the angle bracket quotes are there.&lt;br/&gt;&gt;&lt;br/&gt;&gt; On 12/22/2008 3:45 PM, &gt; sbartfast@somedomain.com wrote:&lt;br/&gt;&gt;&gt; Please someone, reply to this.&lt;br/&gt;&gt;&gt;&lt;br/&gt;&gt;&lt;br/&gt;&lt;/div&gt;" </code></pre> <p>In the above string, none of the > characters that are part of HTML tags should be converted to &gt;. So, this:</p> <pre><code>&lt;div class"quotedReply"&gt;&gt; </code></pre> <p>should become this:</p> <pre><code>&lt;div class"quotedReply"&gt;&amp;gt; </code></pre> <p>Another issue is that the expression above uses a non-capturing group, which is fine except for the fact that the match is in group 1. I'm not quite sure how to do a replace only on group 1 and preserve the rest of the match. It appears that a MatchEvaluator doesn't really do the trick, or perhaps I just can't envision it right now. </p> <p>I suspect my regex could do with some lovin'.</p> <p>Anyone have any bright ideas?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload