是否有方法解码在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;
}
答案 0 :(得分:0)
不,你必须自己实现。原因是这种方法没有用意!对于您可能想要实现的目标,为什么不使用
HttpServerUtility.HtmlEncode(...)
和
HttpServerUtility.HtmlDecode(...)
答案 1 :(得分:0)
我使用HttpUtility.UrlEncode代替HttpUtility.JavaScriptStringEncode,然后 HttpUtility.UrlDecode。
答案 2 :(得分:-1)
您可以使用
HttpUtility.UrlDecode
http://msdn.microsoft.com/en-us/library/system.web.httputility.urldecode(v=vs.110).aspx
也在这里回答:Unescape JavaScript's escape() using C#
但是,UrlDecode在某些字符周围有一些明显的限制(比如+符号,javascript没有unescape)和任何字符值&gt; = 128.使用Microsoft.JScript.GlobalObject.unescape可能是最可靠的,但我不知道它的表现如何(即支持语言是什么。我认为它很快,因为它在这一点上是一个lib。)