在特定字符jquery之后删除引号中的字符

时间:2013-04-23 08:56:59

标签: javascript jquery string replace

我有一个像这样的字符串

{'Salary':'Basic_Salary - ( ESI * .5 ) - Employee_PF + Bonus','Gross_Salary':'( Salary - Tax )','Employee_PF': ''}

如果我有另一个字符串'Salary':'',

我想替换Salary并获得结果

{'Salary':'Basic_Salary - ( ESI * .5 ) - Employee_PF + Bonus','Gross_Salary':'( Salary - Tax )','Employee_PF': ''}

我可以轻松检查'Salary'。我坚持的是,如何在''

之后替换'Salary':中的字符串

2 个答案:

答案 0 :(得分:1)

您基本上使用JSON对象作为字符串,在这种情况下,您可以将此字符串解码为JavaScript对象,然后将属性修改为任何其他对象

var obj = jQuery.parseJSON({'Salary':'Basic_Salary - ( ESI * .5 ) - Employee_PF + Bonus','Gross_Salary':'( Salary - Tax )','Employee_PF': ''});

然后:

obj.Salary = "Whatever you like";

然后你总是可以用

将它转换回字符串
JSON.stringify

答案 1 :(得分:1)

var str = "{'Salary':'Basic_Salary - ( ESI * .5 ) - Employee_PF + Bonus','Gross_Salary':'( Salary - Tax )','Employee_PF': ''}";
var obj = JSON.parse(str);
    obj.Salary = '';
str = JSON.stringify(obj);