【问题标题】:How to unescape the quotes in html string如何取消转义html字符串中的引号
【发布时间】:2019-05-08 19:30:16
【问题描述】:

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

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

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

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

【问题讨论】:

  • 有没有可能修复它的另一面?
  • 您可以strings.Replace(s, "\\", "", -1),但这可能会根据字符串的引用方式而中断。字符串是如何被引用的(Go 字符串字面量引用,JS 字符串字面量引用,...)?
  • 你试过什么?包括你的代码。你遇到了什么问题?

标签: go html-escape-characters


【解决方案1】:

使用strings.NewReplacer()

func NewReplacer(oldnew ...string) *Replacer

package main

    import (
        "bytes"
        "fmt"
        "log"
        "strings"

        "golang.org/x/net/html"
    )

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

输出:

www.google.com

【讨论】:

    【解决方案2】:

    我想得到不带反斜杠的字符串。

    这是一个简单的问题,但现有的两个答案对于这样简单的问题来说都太复杂了。

    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func main() {
        s := `Hello world ! <a href=\"www.google.com\">Google</a>`
        fmt.Println(s)
        fmt.Println(strings.Replace(s, `\"`, `"`, -1))
    }
    

    试试https://play.golang.org/p/7XX7jJ3FVFt

    HTH

    【讨论】:

      【解决方案3】:

      假设您使用的是html/template,您要么将整个内容存储为template.HTML,要么将url 存储为template.URL。你可以在这里看到如何做到这一点: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;
      

      【讨论】:

      • 有可能 OP 指的是使用模板,但问题没有表明是这种情况,这是一个很大的假设。
      猜你喜欢
      • 1970-01-01
      • 2019-03-02
      • 1970-01-01
      • 1970-01-01
      • 2010-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多