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

如何在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)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大