我有一个项目,其中评估了一些 JavaScript 变量。因为字符串需要转义(仅限单引号),所以我在测试函数中编写了完全相同的代码。我有以下一些非常简单的 JavaScript 代码:
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);
}
我想提醒它,说:fsdsd'4565sd
。