首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >javascript正则表达式将替换字符串中的所有双四元组,除非双四元组后跟空格或逗号

javascript正则表达式将替换字符串中的所有双四元组,除非双四元组后跟空格或逗号
EN

Stack Overflow用户
提问于 2013-06-21 02:33:29
回答 5查看 804关注 0票数 0

我有一个问题,有人没有正确地转义值中的双引号,以便稍后解释为JSON字符串。

字符串示例:

代码语言:javascript
复制
{"description":"This is my 12" pizza I ordered.","value":"1"}

当我尝试让JSON.parse()处理这个问题时,由于未转义的双引号(指以英寸为单位的大小),它会给出一个错误。

一开始,我想--就这么做吧:

代码语言:javascript
复制
string.replace(/\"/g,'\"');

当然,这也会转义所有有效的双引号。所以,我不是正则表达式方面的专家,但我四处寻找了一些答案,我认为这需要负向预测?

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

我知道这不是一个完全通用的解决方案,(让人来解决他们的问题),但不幸的是,我没有奢侈的通用解决方案。

提亚

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

在每个双等分之前和之后?我想这就是我想问的--这可以用正则表达式的前视/后视来完成吗?

EN

回答 5

Stack Overflow用户

发布于 2013-06-21 04:08:17

下面是我能做的最好的事情,利用在JSON中未转义的引号只能出现在某些地方这一事实。

代码语言:javascript
复制
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)

产生

代码语言:javascript
复制
{"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不同,您试图解析的语法是不明确的。

票数 1
EN

Stack Overflow用户

发布于 2013-06-21 02:39:42

一种方法:重建json字符串:

代码语言:javascript
复制
var str = '{"description":"This is my 12" pizza I ordered.","value":"1"}';
var regex = /"(.*?)"(?=\s*([,:])\s*"|(}))/g;
var result = '{';

var arr = regex.exec(str);

while (arr != null) {
    result += '"' + arr[1].replace(/\\?"/g, '\\"') + '"';
    if (arr[2]) result += arr[2];
    if (arr[3]) result += arr[3];
    arr = regex.exec(str);
}

console.log(result);
票数 0
EN

Stack Overflow用户

发布于 2013-06-21 02:41:54

不是一个单一的正则表达式,但我认为这样做更安全:

代码语言:javascript
复制
 json_string = '{"description":"This is my 12" pizza: which can also contain other "," which would break in a one liner regex.","value":"1"}';
 console.log(json_string);

 // save the value for later use
 var value = json_string.match(/"value":"(.+)"}$/)[1];

 // isolate just the description value..

 // remove the ","value... from the end
 var desc = json_string.replace(/","value":".+"}$/, '');

 // remove the opening {"description":" from the description value
 desc = desc.replace(/^{"description":"/, '');

 // any remaining " in the description are unwanted to replace them
 desc = desc.replace(/"/g, '"');
 console.log(desc);

 // now put it all back together - if you wanted too - but really you already have the description and value parsed out of the string
 json_string = '{"description":"'+desc+'","value":"'+value+'"}'

 console.log(json_string);

控制台输出如下所示:

代码语言:javascript
复制
{"description":"This is my 12" pizza: which can also contain other "," which would break in a one liner regex.","value":"1"}

This is my 12" pizza: which can also contain other "," which would break in a one liner regex.

{"description":"This is my 12" pizza: which can also contain other "," which would break in a one liner regex.","value":"1"}

注释如果描述中还包含您可能在正则表达式一行程序中使用的任何模式,则此方法不会中断

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17221173

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档