title: JavaScript - replace word if it occurs otherwise append text to the end of a string
body: <p>I have this script to replace a word:</p>
  
  <pre><code>var url = window.location.toString();
  window.location = url.replace(/(\?sort=name|size)/, 'date');
  </code></pre>
  
  <p>and this one meant to append a text to the end of a string:</p>
  
  <pre><code>var orig = location.href;
  location.replace(orig+'?sort=date');
  </code></pre>
  
  <p>How to combine them into one script so the text only gets appended when the ?sort parameter isn't already there?</p>
  
  <p>The ?sort parameter is supposed to be always at the end of a string, if that matters.</p>