title: String Replace in Lua
body: <p>i am looking to write a function in Lua that replaces all occurrences of one string with another, e.g.:</p>
  
  <pre><code>function string.replace(s, oldValue, newValue)
     return string.gsub(s, oldValue, newValue);
  end;
  </code></pre>
  
  <p>What i need (unless Lua already has a string replace function) is a function to escape Lua <strike>regular expression</strike>pattern strings (unless Lua already has an Escape<strike>RegularExpression</strike>Pattern function)</p>
  
  <p>i've tried to start writing the proper <code>string.replace</code> function:</p>
  
  <pre><code>local function EscapeRegularExpression(pattern)
      -- "." ==&gt; "%."
      local s = string.gsub(pattern, "%." "%%%.");
  
      return s;
  end;
  
  function string.replace(s, oldValue, newValue)
      oldValue = EscapeRegularExpression(oldValue);
      newValue = EscapeRegularExpression(newValue);
  
      return string.gsub(s, oldValue, newValue);
  end;
  </code></pre>
  
  <p>But i cannot easily think of all the Lua <strike>regular expression</strike>pattern keywords that need to be escaped.</p>
  
  <h2>Bonus Example</h2>
  
  <p>Another example that needs a fix might be:</p>
  
  <pre><code>//Remove any locale thousands separator:
  s = string.gsub(s, Locale.Thousand, "");
  
  //Replace any locale decimal marks with a period
  s = string.gsub(s, Locale.Decimal, "%.");
  </code></pre>