dougongnan2167 2018-12-07 03:04
浏览 162

如何取消对HTML字符串中的引号进行转义

I have a string in Go as follow:

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

the quotes was escaped, and I want to get the string without backward slash.

I tried to use the html.UnescapeString but not what I want. Is there any solution about my question.

  • 写回答

2条回答 默认 最新

  • dongtan7639 2018-12-07 05:07
    关注

    Assuming you are using html/template, you either want to store the whole thing as a template.HTML, or store the url as a template.URL. You can see how to do it here: https://play.golang.org/p/G2supatMfhK

    tplVars := map[string]interface{}{
        "html": template.HTML(`Hello world ! <a href="www.google.com">Google</a>"`),
        "url": template.URL("www.google.com"),
        "string": `Hello world ! <a href="www.google.com">Google</a>"`,
    
    }
    t, _ := template.New("foo").Parse(`
    {{define "T"}}
        Html: {{.html}}
        Url: <a href="{{.url}}"/>
        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;
    
    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装