2012-03-02 56 views
3

我需要評估字符串上的轉義序列。 爲了逃避字符串,使用了HttpUtility.JavaScriptStringEncode或類似的東西。 我該如何做相反的轉變?函數的名稱是什麼,它與HttpUtility.JavaScriptStringEncode相反?

下面是一個例子:

 var s = @"otehu""oeuhnoa 
     oaehu 
     oatehu 
     oeu"; 
     var t = HttpUtility.JavaScriptStringEncode(s); 
     var n = Decode(t); 

我需要這樣的功能,解碼,這將使n ==可秒;

+1

是否'UrlDecode '幫忙? – Oded 2012-03-02 10:23:07

+0

沒有一點....... – 2012-03-02 10:58:51

回答

1

我發現這個功能張貼在另一個論壇:

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; 
} 

而且它爲我工作

+0

這段代碼用'.Replace(@「\\」,@「\」)做一些雙重解碼。替換(@「\ t」,「\ t」)。 (@「\ n」,「\ n」)'這可能不是最想要的。 – argaz 2017-01-04 15:41:13

1

這裏是我的實現,對於那些誰正在尋找它

private string JavaScriptStringDecode(string value, bool removeDoubleQuotes) 
      { 
       StringBuilder b = new StringBuilder(); 
       int startIndex = 0; 
       int count = 0; 
       for (int i = 0; i < value.Length; i++) 
       { 
        var c = value[i]; 
        if (c == '\\') 
        { 
         if (count > 0) 
         { 
          b.Append(value, startIndex, count); 
          count = 0; 
         } 

         if (i < value.Length - 1) 
         { 
          var c1 = value[i + 1]; 
          bool ignore = false; 
          char charToAppend; 
          switch (c1) 
          { 
           case 'r': 
            charToAppend = '\r'; 
            break; 
           case 't': 
            charToAppend = '\t'; 
            break; 
           case 'n': 
            charToAppend = '\n'; 
            break; 
           case 'b': 
            charToAppend = '\b'; 
            break; 
           case 'f': 
            charToAppend = '\f'; 
            break; 
           case '\\': 
            charToAppend = '\\'; 
            break; 
           case '\'': 
            charToAppend = '\''; 
            break; 
           case '\"': 
            charToAppend = '\"'; 
            break; 
           case 'u': 
           case 'U': 
            if (i < value.Length - 5) 
            { 
             var style = NumberStyles.HexNumber; 
             var cult = CultureInfo.InvariantCulture; 
             int u; 

             if (Int32.TryParse(value.Substring(i + 2, 4), style, cult, out u)) 
             { 
              charToAppend = ((char)u); 
              i += 4; 
              break; 
             } 
            } 
            charToAppend = '\\'; 
            ignore = true; 
            break; 
           default: 
            charToAppend = '\\'; 
            ignore = true; 

            break; 
          } 
          if (!ignore) 
          { 
           i++; 
          } 
          startIndex = i + 1; 
          b.Append(charToAppend); 
          continue; 
         } 


        } 
        count++; 

       } 
       if (count > 0) 
       { 
        b.Append(value, startIndex, count); 
       } 
       if (removeDoubleQuotes) 
       { 
        if (b.Length > 0) 
        { 
         if (b[0] == '"') 
         { 
          b.Remove(0, 1); 
         } 
         if (b[b.Length - 1] == '"') 
         { 
          b.Remove(b.Length - 1, 1); 
         } 
        } 
       } 
       return b.ToString(); 
      } 
+0

此代碼缺少\ x的ASCII編碼字符處理,但很容易添加。 – argaz 2017-01-04 15:46:49

相關問題