Regular expression to replace quotation marks in HTML tags only

Posted by Cindyydnic on Stack Overflow See other posts from Stack Overflow or by Cindyydnic
Published on 2010-03-15T14:48:50Z Indexed on 2010/03/15 15:19 UTC
Read the original article Hit count: 457

Filed under:
|
|

I have the following string:

<div id="mydiv">This is a "div" with quotation marks</div>

I want to use regular expressions to return the following:

<div id='mydiv'>This is a "div" with quotation marks</div>

Notice how the id attribute in the div is now surrounded by apostrophes?

How can I do this with a regular expression?

Edit: I'm not looking for a magic bullet to handle every edge case in every situation. We should all be weary of using regex to parse HTML but, in this particular case and for my particular need, regex IS the solution...I just need a bit of help getting the right expression.

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET