有没有一种方法可以在 C# 中解码用 HttpUtility.JavaScriptStringEncode() 编码的字符串?
示例编码字符串:
<div class=\"header\"><h2>\u00FC<\/h2><script>\n<\/script>\n
我的临时解决方案是:
public static string JavaScriptStringDecode(string source)
{
// Replace some chars.
var decoded = source.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;
}