字符串替换在javascript中不起作用(带双引号)

时间:2012-07-11 07:31:38

标签: javascript string replace double-quotes

请查看以下代码,如果我不执行" Sanitary"函数中的步骤代码不替换字符串值。

有人可以帮我理解这个吗?

完整代码:

<script type="text/javascript">
function replaceString(orgStr,oldStr,newStr){

//############# Sanitary Steps #############//
oldStr = oldStr .replace(/[ ]+/g,"$");
oldStr = oldStr .replace(/[$]+/g," ");
orgStr = orgStr .replace(/[ ]+/g,"$");
orgStr = orgStr .replace(/[$]+/g," ");
newStr = newStr .replace(/[ ]+/g,"$");
newStr = newStr .replace(/[$]+/g," ");
//############# Sanitary Steps #############//

orgStr = orgStr.replace(oldStr,newStr);
if(orgStr.indexOf(oldStr) != -1){
orgStr = replaceString(orgStr,oldStr,newStr)
}
return orgStr;
}
var fields = ['"Employee Expense Facts"."Total Expense"','"Expense Amount by Expense Type Facts"."Airfare Expense Amount"'];
var selectedField = 0;
var selectedField = 0;
var qry = 'SELECT rcount(1) s_0, "Employee Expenses"."Time"."Date" s_1, "Employee Expenses"."Employee Expense Facts"."Total Expense" s_2 FROM "Employee Expenses" WHERE ("Employee Expense Facts"."Total Expense" IS NOT NULL) ORDER BY 1, 2 ASC NULLS LAST WHERE ("Employee Expense Facts"."Total Expense" IS NOT NULL) ORDER BY 1, 2 ASC NULLS LAST';
qry = qry .replace(/[\n\t\r]+/g," ");
var qry2 = replaceString(qry,""+fields[0],""+fields[1]);
console.log(qry2);

</script>

帮助我理解为什么我需要执行这些卫生步骤??? 我通过试错法找到了解决方案。

2 个答案:

答案 0 :(得分:1)

我的建议是:抛弃所有代码!

现在重新开始,通过正常的formubmit或ajax调用将数据从客户端传递到服务器。现在处理它们的服务器端。

永远记住第一条规则:

1)您绝不能相信所有用户都能按照您的方式行事。

这就是为什么永远不要创建SQL客户端!

答案 1 :(得分:0)

问题出在SQL本身:

SELECT rcount(1) s_0,
       "Employee Expenses"."Time"."Date" s_1,
       "Employee Expenses"."Employee Expense Facts"."Total Expense" s_2 
FROM   "Employee Expenses" 
WHERE ("Employee Expense Facts"."Total Expense" IS NOT NULL)
       ORDER BY 1, 2 ASC NULLS LAST 
WHERE ("Employee Expense Facts"."Total Expense" IS NOT NULL)
       ORDER BY 1, 2 ASC NULLS LAST

我发现有双引号即使在使用转义字符后也没有被替换。我尝试用特殊字符替换(“),然后执行字符串替换,但却无法成功完成。

令人惊讶的是,如果您在本地HTML文件上创建此功能,则无需使用卫生代码。但是当我在服务器上传相同的代码时,它不起作用。为此,我必须设置卫生线。

如果还有其他人弄清楚原因造成这种情况,请告诉我:)

感谢 VX