简体   繁体   English

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

[英]String replace does not work in javascript (with double quotes)

Please look at the following code, If i do not perform the "Sanitary" steps in the function the code does not replace the string values. 请查看以下代码,如果我未执行该函数中的“卫生”步骤,则该代码不会替换字符串值。

can some one help me understand this? 有人可以帮助我理解这一点吗?

Complete code : 完整的代码:

<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>

Help me understand why I need to perform those sanitary steps??? 帮助我了解为什么我需要执行这些卫生步骤??? I found the solution by just trial and error method. 我只是通过反复试验的方法找到了解决方案。

My advise would be: Throw away all that code! 我的建议是:丢弃所有代码!

Now start again, handing the data from the client to the server via a normal formsubmit or an ajax call. 现在再次开始,通过普通的formsubmit或ajax调用将数据从客户端传递到服务器。 Now process them serverside. 现在在服务器端处理它们。

And always remember rule number one: 并永远记住第一条规则:

1) You can never trust all users to behave the way YOU want. 1)您永远不能相信所有用户都能按照自己的方式进行操作。

Thats why never ever create your SQL clientside! 这就是为什么永远不要创建您的SQL客户端!

The Issue is in the SQL itself: 问题出在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

I found out that there are double quotes which even after using escape characters are not replaced. 我发现有双引号,即使使用转义符后也不会替换。 I have tried replacing the (") with special characters and then performing the string replace and yet not able to do that successfully. 我尝试用特殊字符替换(“),然后执行字符串替换,但未能成功完成。

Whats surprising is if you create this function on the local HTML file this works without sanitary code. 令人惊讶的是,如果您在本地HTML文件上创建此函数,则无需使用卫生代码即可运行。 but when i upload the same code on the server it does not work. 但是,当我在服务器上上传相同的代码时,它不起作用。 for that i had to put in place the sanitary lines. 为此,我不得不设置卫生管线。

If any one else figures out why this is caused please do let me know :) 如果还有其他人找出原因,请告诉我:)

thanks vx 谢谢vx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM