Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 103337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:06:29+00:00 2026-05-11T01:06:29+00:00

I’m trying to convert all instances of the > character to its HTML entity

  • 0

I’m trying to convert all instances of the > character to its HTML entity equivalent, >, 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.

Here’s what I have so far:

        public static readonly Regex HtmlAngleBracketNotPartOfTag = new Regex('(?:<[^>]*(?:>|$))(>)', RegexOptions.Compiled | RegexOptions.Singleline); 

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.

This is an example of a test string to parse:

'Ok, now I've got the correct setting.<br/><br/>On 12/22/2008 3:45 PM, jproot@somedomain.com wrote:<br/><div class'quotedReply'>> Ok, got it, hope the angle bracket quotes are there.<br/>><br/>> On 12/22/2008 3:45 PM, > sbartfast@somedomain.com wrote:<br/>>> Please someone, reply to this.<br/>>><br/>><br/></div>' 

In the above string, none of the > characters that are part of HTML tags should be converted to >. So, this:

<div class'quotedReply'>> 

should become this:

<div class'quotedReply'>&gt; 

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.

I suspect my regex could do with some lovin’.

Anyone have any bright ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T01:06:30+00:00Added an answer on May 11, 2026 at 1:06 am

    The trick is to capture everything that isn’t the target, then plug it back in along with the changed text, like this:

    Regex.Replace(str, @'\G((?>[^<>]+|<[^>]*>)*)>', '$1&gt;'); 

    But Anthony’s right: right angle brackets in text nodes shouldn’t cause any problems. And matching HTML with regexes is tricky; for example, comments and CDATA can contain practically anything, so a robust regex would have to match them specifically.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.