英文:
How to convert escape characters in HTML tags?
问题
我们可以使用strconv.Unquote
函数来直接将"\u003chtml\u003e"
转换为"<html>"
。这个函数可以将带有转义字符的字符串转换为对应的字符。在Go语言中,可以这样使用:
package main
import (
"fmt"
"strconv"
)
func main() {
str := "\u003chtml\u003e"
unquotedStr, err := strconv.Unquote(`"` + str + `"`)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println(unquotedStr)
}
这段代码会输出<html>
。这里我们使用了strconv.Unquote
函数来将带有转义字符的字符串转换为对应的字符。注意,我们在调用strconv.Unquote
之前,需要在字符串的前后添加双引号。
英文:
How can we directly convert "\u003chtml\u003e"
to "<html>"
? Conversion of "<html>"
to "\u003chtml\u003e"
is quite easy using json.Marshal()
, but json.Unmarshal()
is quite lengthy and cumbersome. Is there any direct way to do that in golang?
答案1
得分: 3
你可以使用strconv.Unquote()
函数进行转换。
需要注意的是,strconv.Unquote()
只能解析带引号的字符串(例如以引号字符"
或反引号字符`
开头和结尾的字符串),所以我们需要手动添加引号。
示例:
// 重要:使用反引号 `(原始字符串字面量)
// 否则编译器会解析它(解释字符串字面量)!
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()
)不会解码形式为\uxxxx
的Unicode序列,只会解码&#decimal;
或&#xHH;
。
示例:
fmt.Println(html.UnescapeString(`\u003chtml\u003e`)) // 错误
fmt.Println(html.UnescapeString(`&#60;html&#62;`)) // 正确
fmt.Println(html.UnescapeString(`&#x3c;html&#x3e;`)) // 正确
输出结果(在Go Playground上尝试):
\u003chtml\u003e
<html>
<html>
注意2:
还需要注意,如果你编写如下代码:
s := "\u003chtml\u003e"
这个带引号的字符串将被编译器本身解析为一个_解释字符串字面量_,所以你无法真正测试它。要在源代码中指定带引号的字符串,可以使用反引号来指定一个_原始字符串字面量_,或者可以使用_双引号_的解释字符串字面量:
s := "\u003chtml\u003e" // 解释字符串字面量(由编译器解析!)
fmt.Println(s)
s2 := `\u003chtml\u003e` // 原始字符串字面量(不会解析)
fmt.Println(s2)
s3 := "\\u003chtml\\u003e" // 双引号的解释字符串字面量
// (由编译器解析为“单引号”)
fmt.Println(s3)
输出结果:
<html>
\u003chtml\u003e
英文:
You can use the strconv.Unquote()
to do the conversion.
One thing you should be aware of is that strconv.Unquote()
can only unquote strings that are in quotes (e.g. start and end with a quote char "
or a back quote char `
), so we have to manually append that.
Example:
// 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)
Output (try it on the Go Playground):
\u003chtml\u003e
<html>
Note: To do HTML text escaping and unescaping, you can use the html
package. Quoting its doc:
> Package html provides functions for escaping and unescaping HTML text.
But the html
package (specifically html.UnescapeString()
) does not decode unicode sequences of the form \uxxxx
, only &#decimal;
or &#xHH;
.
Example:
fmt.Println(html.UnescapeString(`\u003chtml\u003e`)) // wrong
fmt.Println(html.UnescapeString(`&#60;html&#62;`)) // good
fmt.Println(html.UnescapeString(`&#x3c;html&#x3e;`)) // good
Output (try it on the Go Playground):
\u003chtml\u003e
<html>
<html>
Note #2:
You should also note that if you write a code like this:
s := "\u003chtml\u003e"
This quoted string will be unquoted by the compiler itself as it is an interpreted string literal, so you can't really test that. To specify quoted string in the source, you may use the backtick to specify a raw string literal or you may use a double quoted interpreted string literal:
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)
Output:
<html>
\u003chtml\u003e
答案2
得分: 1
你可以使用fmt
字符串格式化包来实现这个功能。
fmt.Printf("%v","<html>") // 将输出<html>
https://play.golang.org/p/ZEot6bxO1H
英文:
You can use the fmt
string formatting package for this scope.
fmt.Printf("%v","\u003chtml\u003e") // will output <html>
答案3
得分: 1
我认为这是一个常见的问题。这是我解决它的方法。
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": "안녕, 세상(世上). ☺"}
}
希望对某人有所帮助。
英文:
I think it's a common problem. This is how I get it work.
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-
Hope this helps someone.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论