A simple JavaScript function that returns a string with backslashes before characters such as single quote (‘), double quote (“), backslash (\).
function addslashes(str){
str = str.replace(/\\/g,'\\\\');
str = str.replace(/\'/g,'\\\'');
str = str.replace(/\"/g,'\\"');
str = str.replace(/\0/g,'\\0');
return str;
}
And for the opposite:
function stripslashes(str) {
str = str.replace(/\\'/g,'\'');
str = str.replace(/\\"/g,'"');
str = str.replace(/\\0/g,'\0');
str = str.replace(/\\\\/g,'\\');
return str;
}