I have these two scenarios:
console.log(('hello "friend" what\'s up?').replace(/\"/g, '\\"'));
I receive the expected result:
hello "friend" what's up?
But, if I do this:
var val = 'hello "friend" what\'s up?';
val.replace(/\"/g, '\\"');
console.log(val);
I get...
hello "friend" what's up?
(the result needs to be hello \"friend\" what's up?)
The only difference is that the second one uses an already created variable that contains the string. Why doesn't the second scenario actually replace the double quotes with \"?