I need to evaluate escape sequences on the string. To escape the string, HttpUtility.JavaScriptStringEncode or something similar was used. How do I do the opposite transformation?
Here's an example:
var s = @"otehu""oeuhnoa
oaehu
oatehu
oeu";
var t = HttpUtility.JavaScriptStringEncode(s);
var n = Decode(t);
i need such function Decode, which would make n == s;
I found this function posted on another forum:
public static string JavaScriptStringDecode(string source)
{
// Replace some chars.
var decoded = source.Replace(@"\'", "'")
.Replace(@"\""", @"""")
.Replace(@"\/", "/")
.Replace(@"\\", @"\")
.Replace(@"\t", "\t")
.Replace(@"\n", "\n");
// Replace unicode escaped text.
var rx = new Regex(@"\\[uU]([0-9A-F]{4})");
decoded = rx.Replace(decoded, match => ((char)Int32.Parse(match.Value.Substring(2), NumberStyles.HexNumber))
.ToString(CultureInfo.InvariantCulture));
return decoded;
}
And it worked for me