I've got a CMS system and I need to do some auto formatting to the HTML strings before they get served up to the client. So in the database I may have an HTML string like this:
> "<h2>Example Header</h2><p>Here is some text about that
> header.</p><h2>Another Header 2</h2></p>Well I got more information
> here.</p>"
I want to add an ID attribute to every H2 tag that contains the text within the H2 tag with spaces removed, which will be used for anchor links. So the above example would be turned into:
> "<h2 id="ExampleHeader">Example Header</h2><p>Here is some text about that
> header.</p><h2 id="AnotherHeader2">Another Header 2</h2></p>Well I got more
> information here.</p>"
So for every H2 in the string go from:
<h2>Header Example Text Right Here</h2>
To:
<h2 id="HeaderExampleTextRightHere">Header Example Text Right Here</h2>
Spaces removed but otherwise the exact same text. How can I do that with regex?