Say I have a string variable (var str) as follows-
var str
Dude, he totally said that "You Rock!"
Now If I\'
You need to use a global regular expression for this. Try it this way:
str.replace(/"/g, '\\"');
Check out regex syntax and options for the replace function in Using Regular Expressions with JavaScript.