var str = "This is a test string with L'Oreal in it.";
var regex = /'/g;
var output = JSON.stringify(str.replace(regex, "'"));
$('#test').html(output);
You can use a regular expression to sniff out the special character code and replace it back before stringifying it. NOTE: the "g" in the regex above makes the search global so it will replace any instance of "'". The above code should work. Here is a jsfiddle demonstrating. http://jsfiddle.net/pJD9X/1/
EDIT: Alternate whitelist approach. You could possibly create a whitelist of special characters and take an approach that is similar to how underscor.js actually encodes stuff. Only instead of encoding you will be decoding. NOTE: this is possibly a dangerous solution because it allows your code to decode special characters
var str = "This is a test string with L'Oreal in it and an ampersand & in it";
var whiteList = {
"'":"'",
"&":"&"
};
var specialCharDecoder = /'|&/g;
function htmlDecode (string) {
return ('' + string).replace(specialCharDecoder, function (match) {
return whiteList[match];
});
}
var output = htmlDecode(str);