I am trying to replace all the occurrences of character %
and _
.
Say var inputStr = '_temp_temp2%temp3';
I can replace it by var replacedStr = inputStr.replace(/_/g, '\\_').replace(/%/g, '\\%');
And this also gives me the same result var replacedStr = inputStr.replace(/\_/g, '\\_').replace(/\%/g, '\\%');
What is the difference between these two? Is there any significance of putting that extra \
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 \
? I have tried to understand from MDN, but didn't understand it properly.