Just a string. Add \\\' to it every time there is a single quote.
replace
works for the first quote, so you need a tiny regular expression:
str = str.replace(/'/g, "\\'");
var str = "This is a single quote: ' and so is this: '";
console.log(str);
var replaced = str.replace(/'/g, "\\'");
console.log(replaced);
Gives you:
This is a single quote: ' and so is this: '
This is a single quote: \' and so is this: \'