Escaping single quotes in JavaScript string for JavaScript evaluation
I have a project, in which some JavaScript var is evaluated. Because the string needs to be escaped (single quotes only), I have written the exact same code in a test function. I have the following bit of pretty simple JavaScript code: function testEscape() { var strResult = ""; var strInputString = "fsdsd'4565sd"; // Here, the string needs to be escaped for single quotes for the eval // to work as is. The following does NOT work! Help! strInputString.replace(/'/g, "''"); var strTest = "strResult = '" + strInputString + "';"; eval(strTest); alert(strResult); } And I want to alert it, saying: