从字符串化的 JSON 中删除逗号,但不删除逗号分隔数组



我有字符串化的JSON数据,在描述字段中包含逗号。 如果我在数据中有撇号或逗号,AJAX 帖子将失败。
如何从我的var test = JSON.stringify(data)中删除以下内容

test 将打印如下:

[{"var1":"0","description":"this, has, commas"},{"var1":"1","description":"more, commas"}]我可以去掉描述中的逗号,以便 JSON 字符串如下所示:[{"var1":"0","description":"this has commas"},{"var1":"1","description":"more commas"}]
所以留下分隔对象的逗号?
或者更好的...:
[{"var1":"0","description":"this \, has \, commas"},{"var1":"1","description":"more \, commas"}]


更改后,数据需要推送回我的服务器并加载回我的数据库,逗号和撇号需要保持原样.
test.replace(/,/g,"")当然...摆脱分隔对象的逗号,并搞砸了我.
任何人都非常了解正则表达式,这可能建议一种在"},{"之间替换","但"不是"的方法?(双引号用于强调(
感谢您的任何帮助。

如何进行测试.替换为负面的前瞻 - https://regex101.com/r/WtHcuO/2/:

var data = JSON.stringify([{"var1":"0","description":"this, has, commas"},{"var1":"1","description":"more, commas"}]);
var stripped = data.replace(/,(?!["{}[]])/g, "");
console.log(stripped);

或者,如果要保留逗号,但对其进行转义,则可以替换为\,而不是""

var data = JSON.stringify([{"var1":"0","description":"this, has, commas"},{"var1":"1","description":"more, commas"}]);
var stripped = data.replace(/,(?!["{}[]])/g, "\,");
console.log(stripped);

最新更新