javascript regex替换字符串中的所有双引号,除非双引号后跟空格或逗号空格 [英] javascript regex replace all doublequotes in string unless the doublequote is followed by space or comma space

查看:124
本文介绍了javascript regex替换字符串中的所有双引号,除非双引号后跟空格或逗号空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,就是有人后来没有正确转义值中的双引号,以致无法将其解释为JSON字符串.

I have an issue where someone isn't properly escaping double quotes in values later to be interpreted as a JSON string.

字符串示例:

{"description":"This is my 12" pizza I ordered.","value":"1"}

当我尝试让JSON.parse()处理时,由于未转义的双引号(指的是以英寸为单位的大小),因此会出现错误

When I try to have JSON.parse() handle this it gives an error because of the unescaped double quote (refering to the size in inches)

起初,我想-要做:

string.replace(/\"/g,'\"');

但是当然这也会转义所有有效的双引号.因此,我不是正则表达式方面的专家,但是我环顾四周寻找答案,我认为这需要一个否定的前瞻性?

but of course this escapes all the valid double-quotes too. So, I'm not an expert on regex, but I looked around for some answers and I think this requires a negative-lookahead?

有人可以帮忙构造一个正则表达式来查找(替换)任何双引号序列,其中有问题的doubleqoute之后的下一个2个字符序列不是空格吗?

Can someone help construct a regex for finding (for replacement) any sequence of doublequote where the next 2-character sequence following the offending doubleqoute is NOT space-comma?

我知道这不是一个全面的修补程序((要让这个人最终解决这个问题),但是不幸的是,我没有一个全面的修补程序.

I know this isn't a total universal fix, (getting the person to fix on their end would be), but unfortunately I don't have the luxury of a universal fix.

TIA

更新-不考虑示例字符串(仅用于说明).是否可以在每个双引号之前和之后测试是否存在有效的JSON-即查找以下任何字符 ,{[:

Update - instead of considering the example string (used for illustration only). Is it possible to test for the presence of valid JSON before and after each doublequote - ie to look for any of the following characters ,{[:

在每个双引号之前和之后?我想这就是我要问的问题-可以在正则表达式后面使用lookahead/

before and after each doublequote? I guess this is what I was asking - can this be done with lookahead / behind regex?

推荐答案

这是我能做的最好的事情,这是因为在JSON中,未转义的引号只能出现在某些地方.

Here's the best I can do, taking advantage of the fact that in JSON an unescaped quote can only occur in certain places.

input = '{"description":"This is my 12" pizza, and I want "thin crust"","value":"1"}';
console.log(input);
output = input.replace(/{"/g, '_OPEN_').replace(/":"/g, '_COLON_').replace(/","/g, '_COMMA_').replace(/"}/g, '_CLOSE_');
output = output.replace(/"/g, '\\"');
output = output.replace(/_OPEN_/g, '{"').replace(/_COLON_/g, '":"').replace(/_COMMA_/g, '","').replace(/_CLOSE_/g, '"}');
console.log(output)

生产

{"description":"This is my 12" pizza, and I want "thin crust"","value":"1"}
{"description":"This is my 12\" pizza, and I want \"thin crust\"","value":"1"}

您可以用不太可能在输入中出现的字符串替换"OPEN","CLOSE"等,如果您不介意正则表达式是含糊的,甚至可以使用控制字符.但是,正如其他人指出的那样,没有一种解决方案在所有情况下都适用.无论您做什么,描述文字中都会出现一个值,这会使您感到困惑,因为与正确生成的JSON不同,您尝试解析的语法是模棱两可的.

You can replace 'OPEN', 'CLOSE' etc with strings less likely to occur in the input, perhaps even control characters if you don't mind the regexes being cryptic. But as others have noted, there is no solution that will work in all cases. No matter what you do, there is a value that could occur in the description text that will mess you up, because unlike properly generated JSON, the syntax you're trying to parse is ambiguous.

这篇关于javascript regex替换字符串中的所有双引号,除非双引号后跟空格或逗号空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆