I have a invalid json string like this:
[{
"itemID": "1",
"itemTitle": "Mango",
"itemText": "some text here"again text",
"ThumbUrl": "http://someurl.com/mango.jpg",
"itemContent": null
}, {
"itemID": "2",
"itemTitle": "orange",
"itemText": "someother text " again another texther",
"ThumbUrl": "http://someurl.com/orange.jpg",
"itemContent": null
}
]
javascript:
$.get("http://www.someapiurl.com/getdata.php", function(data, status){
//here i want to replace json key value to empty before parsing the json
var json = $.parseJSON(data);
}
I want to change the value of itemText to word empty using regular expression. Could any one help me how to achieve this using regular expression ?Thanks
Note:
-JSON is invalid (That is the way i recive it so i have to correct it before parsing it
-The Json response some time got double quotes in itemText)
-the itemText key value is across multiple lines(mix of unicode and non unicode)and long(not on online line)
Edit: I have used this php regular expression to achive same thing. Could you guys help me convert it to javascript regular expresion?
print_r(preg_replace('/\"itemText\"\:\".*?\"\,/s', '"textTitle$1":"empty",',$json));
Edit2: Finally this replaced itemText with word empty for me in all cases:
data.replace(/("itemText"\s*:\s*")[\s\S]*?ThumbUrl/g, '$1empty","ThumbUrl')
.replace(/("itemText"\s*:\s*").*",/, '$1"')/g:.replace(/("itemText"\s*:\s*").*",/g, '$1"')- does it help?