I am trying to write a JavaScript RegEx that properly adds escape characters to all the single and double quote characters in a string. For example, if I have the following string:
"Hey, how's it going? You're really good at this!"
The code should transform that string to:
"Hey, how\'s it going? You\'re really good at this!"
Below is my code:
var testString = "Hey, how's it going? You're really good at this!";
testString = testString.replace(/'/g, '\'').replace(/"/g, '\"');
alert(testString);
The quotes are currently not being replaced by an escape character.