Converting Confluence markup to a simpler XML format I have a code that converts one xml structure to another. I am removing unnecessary tags and replacing some temporarily. I think I have way too many replace operations and wondering if there is a way to optimize this. Can someone pour in suggestions?

Note: I am not using regex to convert xml to plain text. All I am doing using regex is to do away with certain unwanted tags not supported by target xml format, and temporarily replace a few inline elements and replace them back after converting (so that I dont need to parse the inline elements). After sanitizing the string, I am parsing it using jquery xml parser. 

Here's the code snippet:

  str = str.replace(/\r?\n|\r/g, ''); //Remove all new line characters
  // Replace &lt;strong&gt; tags to retain and convert them in the end
  str = str.replace(/&lt;strong&gt;/g, '(strong)');
  str = str.replace(/&lt;\/strong&gt;/g, '(/strong)'); //
  // Replace &lt;code&gt; tags to retain and convert them in the end
  str = str.replace(/&lt;code&gt;/g, '(code)');
  str = str.replace(/&lt;\/code&gt;/g, '(/code)');
  // Remove these tags as they arent required
  str = str.replace(/&lt;ac:rich-text-body&gt;/g, '');
  str = str.replace(/&lt;\/ac:rich-text-body&gt;/g, '');
  // Remove 'ac:' from macros elements
  str = str.replace(/ac:/g, '');
  str = str.replace(/&lt;\/*span.*?&gt;/g, ''); //Remove all span tags
  str = str.replace(/&lt;\/*div.*?&gt;/g, ''); //Remove all div tags
  str = str.replace(/&lt;br.*?&gt;/g, ''); //Remove br tags
  str = str.replace(/&amp;nbsp;/g, ''); //Remove non-breaking white spaces
  str = str.replace(/&lt;\/*a.*?&gt;/g, ''); //Remove a tags
  str = str.replace(/&lt;\/*u&gt;/g, ''); //Remove u tags
  str = str.replace(/&lt;\/*em&gt;/g, ''); //Remove em tags
You are using the wrong tool for the job, as explained here:

https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags

Use XSLT for this job.