title: Replace all spaces in a search input box with & character
body: <p>I have a search input box on a site.
  If the user enters text in the input box I like to replace all [space]-characters with ' &amp; ' (in words: space&amp;space)</p>
  
  <p>But I have a small mistake in my code because if the user tries to delete text (backspace) then it inserts a ' &amp; ' again if the last char was a replaced space...</p>
  
  <p>See my example on JSFiddler: <a href="http://jsfiddle.net/JFEb4/" rel="nofollow">http://jsfiddle.net/JFEb4/</a></p>
  
  <p>Any ideas?</p>
  
  <p>Code:</p>
  
  <pre><code>$("[name='search']").keyup(function() {
  
      var input = $(this).val().replace(/ &amp; /g, "x");
  
      if (input.indexOf(" ") &gt;= 0) {
          $(this).val(input.replace(' ', ' &amp; ').replace(/x/g, " &amp; "
  
  });
  </code></pre>