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