title: when doing global replace in javascript should I do /\%/g or /%/g
body: <p>I am trying to replace all the occurrences of character <code>%</code> and <code>_</code>.  </p>
  
  <p>Say <code>var inputStr = '_temp_temp2%temp3';</code><br>
  I can replace it by <code>var replacedStr = inputStr.replace(/_/g, '\\_').replace(/%/g, '\\%');</code>  </p>
  
  <p>And this also gives me the same result <code>var replacedStr = inputStr.replace(/\_/g, '\\_').replace(/\%/g, '\\%');</code>  </p>
  
  <p>What is the difference between these two? Is there any significance of putting that extra <code>\</code> in second case? Currently I am getting same output for both the cases, is there any risk of using the first method i.e. without the escape caharacter <code>\</code>? I have tried to understand from <a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions" rel="nofollow noreferrer">MDN</a>, but didn't understand it properly.</p>