如何取消转义html字符串中的引号

我在 Go 中有一个字符串如下:

Hello world ! <a href=\"www.google.com\">Google</a>

引号被转义了,我想得到没有反斜杠的字符串。

我尝试使用html.UnescapeString但不是我想要的。关于我的问题有什么解决办法吗。


繁花如伊
浏览 186回答 3
3回答

HUX布斯

使用 strings.NewReplacer()func NewReplacer(oldnew ...string) *替换器package main&nbsp; &nbsp; import (&nbsp; &nbsp; &nbsp; &nbsp; "bytes"&nbsp; &nbsp; &nbsp; &nbsp; "fmt"&nbsp; &nbsp; &nbsp; &nbsp; "log"&nbsp; &nbsp; &nbsp; &nbsp; "strings"&nbsp; &nbsp; &nbsp; &nbsp; "golang.org/x/net/html"&nbsp; &nbsp; )&nbsp; &nbsp; func main() {&nbsp; &nbsp; &nbsp; &nbsp; const htm = `&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Hello world ! <a href=\"www.google.com\">Google</a>&nbsp; &nbsp; &nbsp; &nbsp; `&nbsp; &nbsp; &nbsp; &nbsp; // Code to get the attribute value&nbsp; &nbsp; &nbsp; &nbsp; var out string&nbsp; &nbsp; &nbsp; &nbsp; r := bytes.NewReader([]byte(htm))&nbsp; &nbsp; &nbsp; &nbsp; doc, err := html.Parse(r)&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; var f func(*html.Node)&nbsp; &nbsp; &nbsp; &nbsp; f = func(n *html.Node) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if n.Type == html.ElementNode && n.Data == "a" {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for _, a := range n.Attr {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out = a.Val&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for c := n.FirstChild; c != nil; c = c.NextSibling {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f(c)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; f(doc)&nbsp; &nbsp; &nbsp; &nbsp; // Code to format the output string.&nbsp; &nbsp; &nbsp; &nbsp; rem := `\"`&nbsp; &nbsp; &nbsp; &nbsp; rep := strings.NewReplacer(rem, " ")&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(rep.Replace(out))&nbsp; &nbsp; }输出 :www.google.com

慕尼黑5688855

假设您正在使用html/template,您要么希望将整个内容存储为template.HTML,要么将 url 存储为template.URL。您可以在此处查看操作方法:https ://play.golang.org/p/G2supatMfhKtplVars := map[string]interface{}{&nbsp; &nbsp; "html": template.HTML(`Hello world ! <a href="www.google.com">Google</a>"`),&nbsp; &nbsp; "url": template.URL("www.google.com"),&nbsp; &nbsp; "string": `Hello world ! <a href="www.google.com">Google</a>"`,}t, _ := template.New("foo").Parse(`{{define "T"}}&nbsp; &nbsp; Html: {{.html}}&nbsp; &nbsp; Url: <a href="{{.url}}"/>&nbsp; &nbsp; String: {{.string}}{{end}}`)t.ExecuteTemplate(os.Stdout, "T", tplVars)//Html: Hello world ! <a href="www.google.com">Google</a>"//Url: <a href="www.google.com"/>//String: Hello world ! &lt;a href=&#34;www.google.com&#34;&gt;Google&lt;/a&gt;&#34;

HUWWW

我想得到没有反斜杠的字符串。这是一个简单的问题,但是对于这样一个简单的问题,现有的两个答案都太复杂了。package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "strings")func main() {&nbsp; &nbsp; s := `Hello world ! <a href=\"www.google.com\">Google</a>`&nbsp; &nbsp; fmt.Println(s)&nbsp; &nbsp; fmt.Println(strings.Replace(s, `\"`, `"`, -1))}在https://play.golang.org/p/7XX7jJ3FVFt试试
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go