栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在HTML标签中转换转义字符?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在HTML标签中转换转义字符?

您可以使用

strconv.Unquote()
进行转换。

您应该注意的一件事是,

strconv.Unquote()
只能取消引用中的字符串(例如,以引号char
"
或反引号char
开头和结尾```),因此我们必须手动附加该字符串。

例:

// important to use backtick ` (raw string literal)// else the compiler will unquote it (interpreted string literal)!s := `u003chtmlu003e`fmt.Println(s)s2, err := strconv.Unquote(`"` + s + `"`)if err != nil {    panic(err)}fmt.Println(s2)

输出(在Go Playground上尝试):

u003chtmlu003e<html>

注意: 要对HTML文本进行转义和转义,可以使用该

html
包。引用其文档:

html包提供用于转义和取消转义HTML文本的功能。

html
包(具体
html.UnescapeString()
)做形式的未解码的unipre序列
uxxxx
,仅
&#decimal;
&#xHH;

例:

fmt.Println(html.UnescapeString(`u003chtmlu003e`)) // wrongfmt.Println(html.UnescapeString(`&#60;html&#62;`))   // goodfmt.Println(html.UnescapeString(`&#x3c;html&#x3e;`)) // good

输出(在Go Playground上尝试):

u003chtmlu003e<html><html>

笔记2:

您还应该注意,如果您编写如下代码:

s := "u003chtmlu003e"

该带引号的字符串将由编译器本身取消引用,因为它是一个 解释后的字符串文字 ,因此您无法真正进行测试。要在源代码中指定带引号的字符串,可以使用反引号指定
原始字符串文字 ,也可以使用 双引号的 解释字符串文字:

s := "u003chtmlu003e" // Interpreted string literal (unquoted by the compiler!)fmt.Println(s)s2 := `u003chtmlu003e` // 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>u003chtmlu003e


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/387470.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号