【问题标题】:Remove empty double quotes from string javascript从字符串 javascript 中删除空的双引号
【发布时间】:2021-06-11 07:04:04
【问题描述】:

您好,我正在尝试清理我拥有的 json 字符串,例如:

      " .tagline-wrapper ":{
         " padding-top":"398px",
         " font-size":" 1.8rem",
         " "
      },
      ".tagline ":{
         " padding-top":"398px",
         " font-size":" 1.8rem",
         " ",
      },
      ".wrapper":{
         " padding-top":"398px",
         " font-size":" 1.8rem",
         ""
      },

我需要删除空的双引号或带有空格的双引号,它可能有多个空格,我尝试删除连续的空格,然后将引号替换为:

                str = str .replace("\" \"",'');
                str = str .replace("\"\"",'');

但没有任何效果,我也应该删除逗号,但我想我可以删除连续逗号忽略空格,但如果你也能提供帮助,那就太好了。我需要的输出是:

" .tagline-wrapper":{
         " padding-top":"398px",
         " font-size":" 1.8rem",
      },
      ".tagline":{
         " padding-top":"398px",
         "font-size":" 1.8rem",
      },
      ".wrapper":{
         " padding-top":"398px",
         " font-size":" 1.8rem",
      },

这样我就可以解析json了

【问题讨论】:

  • 为了在需要下更好一点,请您提供一个示例输入和该示例的所需输出。
  • 您的 json 无效,。理想情况下,您是否需要修复 JSON。
  • @Keith 这将从用户提供的文件中读取,所以我必须清理它,我不能强迫用户给我一个干净的文件
  • 这是一个 X/Y 问题

标签: javascript regex string replace


【解决方案1】:

为什么不使用正则表达式来搜索 " 后跟任意数量的空格,后跟 "" 后跟可选逗号?

const str = `      " .tagline-wrapper ":{
         " padding-top":"398px",
         " font-size":" 1.8rem",
         " "
      },
      ".tagline ":{
         " padding-top":"398px",
         " font-size":" 1.8rem",
         " ",
      },
      ".wrapper":{
         " padding-top":"398px",
         " font-size":" 1.8rem",
         ""
      },`;
      
console.log(str.replace(/"\s*",?/g, ''))

【讨论】:

  • 更准确的版本是这个表达式:/" ?",?/g
【解决方案2】:

我清理了您的字符串并尝试应用 JSON.parse 将其转换为对象。

注意:replaceAll 在 Nodejs 15.x 中可用。

const str = `" .tagline-wrapper ":{
  " padding-top":"398px",
  " font-size":" 1.8rem",
  " "
},
".tagline ":{
  " padding-top":"398px",
  " font-size":" 1.8rem",
  " ",
},
".wrapper":{
  " padding-top":"398px",
  " font-size":" 1.8rem",
  ""
},`;

const cleanStr =
  "{" +
  str
    .replaceAll('" ', '"')
    .replaceAll(' "', '"')
    .replaceAll('""', "")
    .replaceAll(",\n ,", ",\n")
    .replaceAll(",\n\n}", "}")
    .replaceAll(",\n \n}", "}")
    .slice(0, -1) +
  "}";

console.log(cleanStr);
const object = JSON.parse(cleanStr);
console.log(object);

【讨论】:

  • 它确实比其他人更好地清理数据,所以+1。但是,您应该真正研究正则表达式,因为它们可以执行全部替换(使用 g 标志),并且您需要更少的替换。
  • @Lucero,感谢您的建议。以后有时间我会看看,可能会更新答案。
【解决方案3】:

试试这个代码可能对你有帮助

function convertJSON(string) {
    string = string.replaceAll('"', "'");
    string = string.replaceAll(/(\n\s*)/g, '');
    string = string.replaceAll(/(,?\n?\s?'\s*',?)/g, "");
    string = string.replaceAll(/},$/g, "}").replaceAll("'", '"');
    let parsedJSON = null;
    try{
        parsedJSON = JSON.parse(`{${string}}`);
    }
    catch{
        console.log(string);
    }
    return parsedJSON;
}

let str = `" .tagline-wrapper ":{
         " padding-top":"398px",
         " font-size":" 1.8rem",
         " "
      },
      ".tagline ":{
         " padding-top":"398px",
         " font-size":" 1.8rem",
         " ",
      },
      ".wrapper":{
         " padding-top":"398px",
         " font-size":" 1.8rem",
         ""
      },`;

console.log(convertJSON(str))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-15
    • 1970-01-01
    • 2013-10-09
    • 2012-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多