You cannot use >
after you have formatted the XML, because that character no longer exists anywhere in the text.
This will match/replace the opening h2 tag, but not the closing one:
<cfset test = blah.replaceAll('<h2((?:[^&]+|&(?!gt))*)>','<title_2$1>') />
The key part of that is: (?:[^&]+|&(?!gt))*
Which matches either a non-ampersand character, or an ampersand not followed by gt
, until it finds the end of the tag.
To change the whole tag you need:
<cfset test = blah.replaceAll('<h2((?:[^&]+|&(?!gt))*)>((?:[^&]+|&(?!lt;/h2))*)</h2>','<title_2$1>$2</title_2>') />
This repeats the same concept as above to also look for the closing h2 tag whilst capturing the contents to the appropriate groups.
At this stage you're starting to enter the territory of regex probably not being the best tool for the job - can you do these changes with an XML Parser prior to formatting it?