title: Javascript regex to match a pattern but NOT match a regex literal (r.js optimizer and uglify issue)?
body: <p>I've got a Backbone application, organized into modules using Require.js. One of these modules contains a Handlebars helper, which has a method I use to pull a legal header off of all our HTML templates for each View. The header is contained in an HTML comment, so I use the following regex to strip it off:</p>
  
  <pre><code>/&lt;!--[\s\S]*?--&gt;/g
  </code></pre>
  
  <p>Now, when I optimize (concatenate/compile/minify) the application using r.js, I'm doing the same removal of HTML comments using the <code>onBuildWrite()</code> method of r.js:</p>
  
  <pre><code>onBuildWrite: function (moduleName, path, contents) {
      var htmlCommentRegex = /&lt;!--[\s\S]*?--&gt;/g;
  
      return contents.replace(htmlCommentRegex, "");
  },
  </code></pre>
  
  <p>Now, unfortunately, this means that when the Require.js module containing the Handlebars helper is pulled into the r.js optimized build, <strong>the regex literal within the helper is stripped out</strong>, causing my entire r.js build to bomb out.</p>
  
  <p>I've tried to resolve the issue by selectively applying the regex in <code>onBuildWrite()</code> to all modules EXCEPT the helper:</p>
  
  <pre><code>onBuildWrite: function (moduleName, path, contents) {
      var htmlCommentRegex = /&lt;!--[\s\S]*?--&gt;/g;
  
      if (moduleName !== "helpers/handlebars.compileClean") {
          contents = contents.replace(htmlCommentRegex, "");
      }
  
      return contents;
  },
  </code></pre>
  
  <p>But this doesn't appear to work when uglification is enabled in the r.js configuration; <strong>the regex STILL seems to be running on the entire built script</strong>, including the helper, causing the build to bomb out.</p>
  
  <p>If uglify is disabled in the r.js config, everything works fine.</p>
  
  <p><strong>Anyone have any ideas why uglify would break this? Would switching to a different regex, that would capture HTML comments but IGNORE the HTML comment regex literal, solve the issue? If so, what would that regex look like?</strong></p>