Function to escape quotes in JavaScript Function to escape quotes in JavaScript elasticsearch elasticsearch

Function to escape quotes in JavaScript


query_str = query_str.replace(/"/g, '\\\"');

..will result in; " to \"

OR

query_str = query_str.replace(/"/g, '\\\\\"');

..will result in; " to \\", which will make a printed quotation still be escaped to \".

This code;

var test = 'asdasd " asd a "';console.log(test.replace(/"/g, '\\\"'));console.log(test.replace(/"/g, '\\\\\"'));

..outputs;

asdasd \" asd a \"asdasd \\" asd a \\"

You might adjust the replacement based on how your final interpreter reads the string and prints it out.