【问题标题】:Javascript regex replace character if preceded AND followed by a letterJavascript 正则表达式替换字符,如果前面和后跟一个字母
【发布时间】:2022-01-10 17:55:22
【问题描述】:

基本上,我正在处理一个字符串,它是 python 中的 json 字符串,但在 javascript 中使用时,它有“'”标签而不是双引号,我想将它变成一个真正的 json(通过使用 JSON. parse()) 但是句子中间有一些引号(因为我把“'”换成了双引号)。

示例:'{"author": "Jonah D"Almeida", ... }' (我想替换 D 和 Almeida 之间的那个)

由于整个句子周围已经有引号,javascript给出了一个错误,因为它不能从中创建一个json,所以,为了解决这个问题,基本上我想将句子中间的引号替换为' (单个标记),但前提是它在引号前后有字母。

我的想法:myString.replace('letter before ... " ... letter after', "'")

知道如何获得正确的表达方式吗?基本上我只是想知道正则表达式检查 " 引号前后是否有字母,如果是,请将其更改为单标记 (')。

【问题讨论】:

  • 解析 valid JSON 数据将返回一个完全有效的对象,如果是 OP 的用例,也会返回一个正确转义的字符串值。
  • 这种无效的 JSON 格式从何而来?
  • 我在 python 中有一个有效的 json,但是当我在 javascript 中使用它时,它带有 '''我必须用“替换的部分。但由于其中一个替换在句子内,我想用'而不是“替换它
  • 我根据 OP 的清理要求编辑了我的答案,这些要求是......替换和/或转义(?)任何 "[双引号] 字符 [which is] 前后都是信”

标签: javascript json regex


【解决方案1】:

OP ... “基本上我正在使用一个 json 字符串”

上面的例子不是 OP 所说的 json 字符串。 OP 的示例数据字符串已经是无效的 JSON。

因此,首先要修复生成此类数据的过程。

因为……

“解析 valid JSON 数据将返回一个完全有效的对象,如果是 OP 的用例,还会返回一个正确转义的字符串值。”

...证明...

const testSample_A = { author: "Jonah D'Almeida" };
const testSample_B = { author: 'Jonah D"Almeida' };
const testSample_C = { author: 'Jonah D\'Almeida' };
const testSample_D = { author: "Jonah D\"Almeida" };

console.log({
  testSample_A,
  testSample_B,
  testSample_C,
  testSample_D,
});
console.log('JSON.stringify(...) ... ', {
  testSample_A: JSON.stringify(testSample_A),
  testSample_B: JSON.stringify(testSample_B),
  testSample_C: JSON.stringify(testSample_C),
  testSample_D: JSON.stringify(testSample_D),
});
console.log('JSON.parse(JSON.stringify(...)) ... ', {
  testSample_A: JSON.parse(JSON.stringify(testSample_A)),
  testSample_B: JSON.parse(JSON.stringify(testSample_B)),
  testSample_C: JSON.parse(JSON.stringify(testSample_C)),
  testSample_D: JSON.parse(JSON.stringify(testSample_D)),
});
.as-console-wrapper { min-height: 100%!important; top: 0; }

编辑

完全符合 OP 要求的清理任务仍然可以基于正则表达式来实现,该正则表达式具有正数 lookahead 和正数 lookbehind ... 987654324@.../(?<=\p{L})"(?=\p{L})/gmu

console.log('Letter unicode escapes ...', `
  {"author": "Jonah D"Almeida", ... }
  {"author": "Jon"ah D"Almeida", ... }
  {"author": "Jon"ah D"Alme"ida", ... }`
    .replace(/(?<=\p{L})"(?=\p{L})/gmu, '\\"')
);
console.log('Basic Latin support ...', `
  {"author": "Jonah D"Almeida", ... }
  {"author": "Jon"ah D"Almeida", ... }
  {"author": "Jon"ah D"Alme"ida", ... }`
    .replace(/(?<=\w)"(?=\w)/gm, '\\"')
);

console.log(
  'sanitized and parsed string data ...',
  JSON.parse(`[
    { "author": "Jonah D"Almeida" },
    { "author": "Jon"ah D"Almeida" },
    { "author": "Jon"ah D"Alme"ida" }
  ]`.replace(/(?<=\p{L})"(?=\p{L})/gmu, '\\"'))
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

【讨论】:

  • 你做的最后一个版本正是我要找的东西。但是我将要替换的字母从 ' \\" ' 更改为 " ' " 因为我只想在“句子内部”引号中将双引号更改为单引号。非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多