亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何轉換 HTML 標簽中的轉義字符?

如何轉換 HTML 標簽中的轉義字符?

Go
胡說叔叔 2022-01-04 10:34:07
我們如何直接轉換"\u003chtml\u003e"為"<html>"?使用 轉換"<html>"to"\u003chtml\u003e"很容易json.Marshal(),但json.Unmarshal()相當冗長和繁瑣。在 golang 中有沒有直接的方法可以做到這一點?
查看完整描述

3 回答

?
米琪卡哇伊

TA貢獻1998條經驗 獲得超6個贊

您可以使用strconv.Unquote()來進行轉換。


您應該注意的一件事是,strconv.Unquote()只能取消引號中的字符串(例如,以引號字符"或反引號字符開頭和結尾`),因此我們必須手動附加它。


例子:


// Important to use backtick ` (raw string literal)

// else the compiler will unquote it (interpreted string literal)!


s := `\u003chtml\u003e`

fmt.Println(s)

s2, err := strconv.Unquote(`"` + s + `"`)

if err != nil {

    panic(err)

}

fmt.Println(s2)

輸出(在Go Playground上試試):


\u003chtml\u003e

<html>

注意:要進行 HTML 文本轉義和反轉義,您可以使用html包。引用它的文檔:


包 html 提供轉義和取消轉義 HTML 文本的功能。


但是html包(特別是html.UnescapeString())不解碼形式的 unicode 序列\uxxxx,只有&#decimal;或&#xHH;。


例子:


fmt.Println(html.UnescapeString(`\u003chtml\u003e`)) // wrong

fmt.Println(html.UnescapeString(`&#60;html&#62;`))   // good

fmt.Println(html.UnescapeString(`&#x3c;html&#x3e;`)) // good

輸出(在Go Playground上試試):


\u003chtml\u003e

<html>

<html>

筆記2:


您還應該注意,如果您編寫這樣的代碼:


s := "\u003chtml\u003e"

這個帶引號的字符串將被編譯器本身取消引用,因為它是一個解釋過的字符串文字,所以你不能真正測試它。要在源代碼中指定帶引號的字符串,您可以使用反引號來指定原始字符串文字,或者您可以使用雙引號解釋的字符串文字:


s := "\u003chtml\u003e" // Interpreted string literal (unquoted by the compiler!)

fmt.Println(s)


s2 := `\u003chtml\u003e` // Raw string literal (no unquoting will take place)

fmt.Println(s2)


s3 := "\\u003chtml\\u003e" // Double quoted interpreted string literal

                           // (unquoted by the compiler to be "single" quoted)

fmt.Println(s3)

輸出:


<html>

\u003chtml\u003e


查看完整回答
反對 回復 2022-01-04
?
12345678_0001

TA貢獻1802條經驗 獲得超5個贊

我認為這是一個普遍的問題。這就是我讓它工作的方式。


func _UnescapeUnicodeCharactersInJSON(_jsonRaw json.RawMessage) (json.RawMessage, error) {

    str, err := strconv.Unquote(strings.Replace(strconv.Quote(string(_jsonRaw)), `\\u`, `\u`, -1))

    if err != nil {

        return nil, err

    }

    return []byte(str), nil

}


func main() {

    // Both are valid JSON.

    var jsonRawEscaped json.RawMessage   // json raw with escaped unicode chars

    var jsonRawUnescaped json.RawMessage // json raw with unescaped unicode chars


    // '\u263a' == '?'

    jsonRawEscaped = []byte(`{"HelloWorld": "\uC548\uB155, \uC138\uC0C1(\u4E16\u4E0A). \u263a"}`) // "\\u263a"

    jsonRawUnescaped, _ = _UnescapeUnicodeCharactersInJSON(jsonRawEscaped)                        // "?"


    fmt.Println(string(jsonRawEscaped))   // {"HelloWorld": "\uC548\uB155, \uC138\uC0C1(\u4E16\u4E0A). \u263a"}

    fmt.Println(string(jsonRawUnescaped)) // {"HelloWorld": "??, ??(世上). ?"}

}

https://play.golang.org/p/pUsrzrrcDG-


希望這可以幫助某人。


查看完整回答
反對 回復 2022-01-04
?
慕蓋茨4494581

TA貢獻1850條經驗 獲得超11個贊

您可以fmt為此范圍使用字符串格式化包。

fmt.Printf("%v","\u003chtml\u003e") // will output <html>

https://play.golang.org/p/ZEot6bxO1H


查看完整回答
反對 回復 2022-01-04
  • 3 回答
  • 0 關注
  • 603 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號