(FAIL) Replace two hyphens with an em dash

A wrong example of how to replace the same text in a paragraph, especially when the same text occurs more than once. Notice in this example the second pair of hyphens are not replaced with a em dash.

by Zane Ng

HTML

<div class="text_div">
  <p>This--I mean this--is a test.</p>
</div>

JavaScript

$('p').contents().filter(function() {
  return this.nodeType == 3
}).each(function() {
  this.textContent = this.textContent.replace('--', '—');
});