0
我有一个JavaScript正则表达式来修复损坏的JSON对象(我的后端从JSON字符串中删除所有引号,正则表达式再次添加它们)。翻译Javascript正则表达式到Python
var src = '[{ key: any text with spaces, emptykey: , foo: 0}, { key2: other text with spaces, emptykey2: , foo2: 2},]';
console.log(src.replace(/(\w+):(\s*)(.*?)(,|})/g, '"$1":$2"$3"$4'));
// outputs [{ "key" : "any text with spaces", emptykey: "", "foo": "0"},...]
我需要翻译这个正则表达式替换为python,但我不知道如何包含具有名称后向引用的部分。这里是我的出发点
import json
import re
invalid_json = '[{ key: any text with spaces, emptykey: , foo: 0}, { key2: other text with spaces, emptykey2: , foo2: 2}]'
result = re.sub('/(\w+):(\s*)(.*?)(,|})/g', what to do here in python?, invalid_json)
print result
@MohammadYusufGhazi我将如何使用JavaScript占位符蟒蛇$ 1吗? – ManuKaracho
用\\ 1替换$ 1 – Zaphod
@ManuKaracho'r'\ 1''或''\\ 1'' – MYGz