dslfq06464
2019-03-07 04:34
采纳率: 0%
浏览 265
已采纳

如何在golang中用空字符串替换所有html标签

I'm trying to replace all html tag such as <div> </div> ... on empty string ( " " ) in golang with regex pattern ^[^.\/]*$/g to match all close tag. ex : </div>

My solution:

package main

import (
    "fmt"
    "regexp"
)

const Template = `^[^.\/]*$/g`

func main() {
    r := regexp.MustCompile(Template)
    s := "afsdf4534534!@@!!#<div>345345afsdf4534534!@@!!#</div>"

    res := r.ReplaceAllString(s, "")
    fmt.Println(res)
}

But output the same source string. What's wrong? Please help. Thank

Expect Result should: "afsdf4534534!@@!!#345345afsdf4534534!@@!!#"

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanhanglekr37902 2019-03-07 04:51
    已采纳

    if you want replace all HTML TAG, using strip of html tag.

    regex to match HTML tags is not good idea.

    package main
    
    import (
        "fmt"
        "github.com/grokify/html-strip-tags-go"
    )
    
    func main() {
        text := "afsdf4534534!@@!!#<div>345345afsdf4534534!@@!!#</div>"
    
        stripped := strip.StripTags(text)
    
        fmt.Println(text)
        fmt.Println(stripped)
    }
    
    打赏 评论

相关推荐 更多相似问题