简体   繁体   English

使用 Regex/Replace 将转义引号替换为引号,除非它们位于替换后的字符串中

[英]Using Regex/Replace to replace escaped quotes with quotes, unless they would be in a string after the replace

I am receiving JSON from an API. Unfortunately, all the nested objects are returned as strings.我从 API 收到 JSON。不幸的是,所有嵌套对象都作为字符串返回。 I am trying to use .replace() to convert the string to a JSON object. So "{ -> { , }" -> } and \" -> " .我正在尝试使用.replace()将字符串转换为 JSON object。所以"{ -> { , }" -> }\" -> "

Original issue is when a value actually needs to have escaped quotes they are replaced by regular quotes causing JSON syntax error.最初的问题是当一个值实际上需要转义引号时,它们会被常规引号替换,从而导致 JSON 语法错误。 I almost have it worked it out, albeit in a seemingly clumsy way, by just using some things I know will remain consistent about the data.我几乎已经解决了这个问题,尽管是以一种看似笨拙的方式,只是使用了一些我知道会与数据保持一致的东西。 Here is how I have it working now:这是我现在的工作方式:

Example of a value which should be a nested object but is actually a string (Ignore all newlines just adding for better readability):一个应该是嵌套的 object 但实际上是一个字符串的值的示例(忽略所有为了更好的可读性而添加的换行符):

"title": "{
    \"en\": \"Who is Dwayne \"the rock\" Johnson?\",
    \"es\": \"Quien es Dwayne \"la roca\" Johnson?\"
}"

My replace calls:我的替换电话:

.replace(/"\s*{/gim, '{') // removes quote from opening bracket
.replace(/\\"en\\"/gim, '"en"') // removes escape character from "en"
.replace(/\\"es\\"/gim, '"es"') // removes escape character from "es"
.replace(/:\s*\\"/gim, ':"') // removes escape character from " following :
.replace(/\\"\s*,/gim, '",') // removes escape character from " preceding ,
.replace(/\\"\s*}\s*"/gim, '"}') // removes escape character for quote preceding } and removes quote from closing bracket

For that example this works outputting the desired:对于该示例,这可以输出所需的内容:

"title": {
    "en": "Who is Dwayne \"the rock\" Johnson?",
    "es": "Quien es Dwayne \"la roca\" Johnson?"
}

However, my solution relies on the colons and commas to know which escaped quotes should be replaced with regular quotes.但是,我的解决方案依赖于冒号和逗号来知道哪些转义引号应该替换为常规引号。 If the actual content were to have an escaped quote followed by a comma for example, this would break:例如,如果实际内容有一个转义引号后跟一个逗号,这将中断:

"title": "{
    \"en\": \"Who is Dwayne \"the rock\", Johnson?\",
    \"es\":\"Quien es Dwayne \"la roca\", Johnson?\"
}"

Same if an escaped quote in the actual content were preceded by a colon.如果实际内容中的转义引号前面有冒号,则相同。 I've tested around with lookahead and lookbehind but I'm not sure that will work as the characters around needed/unneeded escapes are pretty much the same.我已经用前瞻和后视进行了测试,但我不确定这是否会起作用,因为需要/不需要的转义周围的字符几乎相同。

Is this possible to do with regex and replace expressions and if so how?这可能与正则表达式和替换表达式有关吗?如果可以,如何处理?

I think your nested JSON example has an escape issue, it should be:我认为您的嵌套 JSON 示例存在转义问题,应该是:

"title": "{
  \"en\": \"Who is Dwayne \\\"the rock\\\" Johnson?\",
  \"es\": \"Quien es Dwayne \\\"la roca\\\" Johnson?\"
}"

Ignore newlines, added for clarity.忽略换行符,为清楚起见添加。 So the title value is a nested JSON string, where:所以title值是一个嵌套的 JSON 字符串,其中:

  • " is escaped as \" "转义为\"
  • \ is escaped as \\ \转义为\\
  • \" is escaped as \\\" (eg \\ and \" ) \"转义为\\\" (例如\\\"

With this you can parse the nested JSON like this, eg no need to use regex:有了这个,您可以像这样解析嵌套的 JSON,例如,无需使用正则表达式:

 let jsonString = `{ "someKey": "someValue", "someNum": 42, "title": "{\\"en\\": \\"Who is Dwayne \\\\\\"the rock\\\\\\" Johnson?\\",\\"es\\": \\"Quien es Dwayne \\\\\\"la roca\\\\\\" Johnson?\\"}" }`; console.log('jsonString:', jsonString); let obj = JSON.parse(jsonString); console.log('obj:', obj); let titleObj = JSON.parse(obj.title); console.log('titleObj:', titleObj); console.log('titleObj.en value:', titleObj.en);

Output: Output:

jsonString: {
  "someKey": "someValue",
  "someNum": 42,
  "title": "{\"en\": \"Who is Dwayne \\\"the rock\\\" Johnson?\",\"es\": \"Quien es Dwayne \\\"la roca\\\" Johnson?\"}"
}
obj: {
  "someKey": "someValue",
  "someNum": 42,
  "title": "{\"en\": \"Who is Dwayne \\\"the rock\\\" Johnson?\",\"es\": \"Quien es Dwayne \\\"la roca\\\" Johnson?\"}"
}
titleObj: {
  "en": "Who is Dwayne \"the rock\" Johnson?",
  "es": "Quien es Dwayne \"la roca\" Johnson?"
}
titleObj.en value: Who is Dwayne "the rock" Johnson?

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

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